import exposing (TreePath, atTrunk).
Stay at that tree. Don't go anywhere.
TreePath.atTrunk
--> TreePath.go []
TreePath.atTrunk
|> TreePath.depth
--> 0Conveniently describe the route to go from the trunk.
TreePath.go [ 2, 4, 0 ]
--> [ 2, 4, 0 ] or
--> TreePath.atTrunk
--> |> TreePath.toChild 2
--> |> TreePath.toChild 4
--> |> TreePath.toChild 0On which level of sub-branch is the final tree located?
TreePath.depth TreePath.atTrunk
--> 0
TreePath.atTrunk
|> TreePath.toChild 5
|> TreePath.toChild 2
|> TreePath.toChild 0
|> TreePath.depth
--> 3Does this path lead to a child or child of a child or ... of the tree at this path?
TreePath.go [ 2, 3, 0 ]
|> TreePath.goesToChildOf
(TreePath.go [ 2, 3 ])
--> True
TreePath.go [ 2 ]
|> TreePath.goesToChildOf
(TreePath.go [ 2, 3 ])
--> FalseDoes this path lead to its parent or the parent of its parent or ... of the tree at this path?
TreePath.go [ 2 ]
|> TreePath.goesToParentOf
(TreePath.go [ 2, 3 ])
--> True
TreePath.go [ 2, 3, 0 ]
|> TreePath.goesToParentOf
(TreePath.go [ 2, 3 ])
--> FalseGo to the next child index.
TreePath.go [ 2, 4, 0 ]
|> TreePath.step
--> Just ( 2, TreePath.go [ 4, 0 ] )
TreePath.atTrunk
|> TreePath.step
--> Nothing
at : TreePath -> Tree a -> Maybe (Tree a)
at path =
case TreePath.step path of
Nothing ->
Just
Just ( index, further ) ->
Tree.children
>> List.getAt index
>> Maybe.andThen (at further)The TreePath to its parent tree – if there is one.
TreePath.go [ 1, 2, 3 ]
|> TreePath.toParent
--> Just (TreePath.go [ 1, 2 ])
TreePath.atTrunk
|> TreePath.toParent
--> NothingThe path to its ...th child from there.
TreePath.atTrunk
|> TreePath.toChild 4
|> TreePath.toChild 1
|> TreePath.toChild 8
--> TreePath.go [ 4, 1, 8 ]This is often useful in recursive functions.
view path tree =
button [ onPress (Clicked path) ]
:: (Tree.children tree
|> List.indexedMap
(\index ->
view (path |> TreePath.toChild index)
)
)
|> column [ Ui.padding (10 / TreePath.depth path) ]
The unique location of a child-
Treein aTree.You describe which branch index to go along every step.