Comment detail

フォルダパス一覧のツリー構造への変換 (Nested Flatten)
Squeak Smalltalk で。

mamamoto さんの #4473 に感じ入ったあとでは、#groupBy:having: を便利に使っているつもりが使われてしまっているみたいでダメダメですね(^_^;)。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| genTree printTree pathList tree |
genTree := [:paths |
    | leaf |
    leaf := paths groupBy: [:path | path first] having: [:g | true].
    leaf associationsDo: [:assoc |
        | newValue |
        newValue := assoc value
            collect: [:val | val allButFirst] thenSelect: [:val | val notEmpty].
        assoc value: (newValue isEmpty
            ifTrue: [newValue]
            ifFalse: [genTree copy fixTemps value: newValue])]].

printTree := [:dic :lev |
    dic keys asSortedCollection do: [:key |
        Transcript crtab: lev; show: key.
        (dic at: key) ifNotEmptyDo: [:child |
            printTree copy fixTemps value: child value: lev + 1]]].

pathList := #('abc\def' 'abc\def\gh' 'abc\def\ij' 'abc\jk\lm' 'de').
pathList := pathList collect: [:each | each subStrings: {$\}].

World findATranscript: nil.
tree := genTree copy fixTemps value: pathList.
printTree copy fixTemps value: tree value: 0

"=>
abc
    def
        gh
        ij
    jk
        lm
de
"

Index

Feed

Other

Link

Pathtraq

loading...