Location in a Tree
Stay at that tree. Don't go anywhere.
Tree.Path.atTrunk
--> Tree.Path.follow []
Tree.Path.atTrunk
|> Tree.Path.depth
--> 0Conveniently describe the route to go from the trunk.
Tree.Path.follow [ 2, 4, 0 ]
--> [ 2, 4, 0 ]
-- or
Tree.Path.follow [ 2, 4, 0 ]
--> Tree.Path.atTrunk
--> |> Tree.Path.toChild 2
--> |> Tree.Path.toChild 4
--> |> Tree.Path.toChild 0On which level of sub-branch is the final tree located?
Tree.Path.depth Tree.Path.atTrunk
--> 0
Tree.Path.atTrunk
|> Tree.Path.toChild 5
|> Tree.Path.toChild 2
|> Tree.Path.toChild 0
|> Tree.Path.depth
--> 3Does this path lead to a child or child of a child or ... of the tree at this path?
Tree.Path.follow [ 2, 3, 0 ]
|> Tree.Path.targetsChildOf
(Tree.Path.follow [ 2, 3 ])
--> True
Tree.Path.follow [ 2 ]
|> Tree.Path.targetsChildOf
(Tree.Path.follow [ 2, 3 ])
--> FalseDoes this path lead to its parent or the parent of its parent or ... of the tree at this path?
Tree.Path.follow [ 2 ]
|> Tree.Path.targetsParentOf
(Tree.Path.follow [ 2, 3 ])
--> True
Tree.Path.follow [ 2, 3, 0 ]
|> Tree.Path.targetsParentOf
(Tree.Path.follow [ 2, 3 ])
--> FalseTry to navigate to a child index, returning a ForestPath for the children.
import Tree exposing (Tree)
import Tree.Path exposing (TreePath)
Tree.Path.follow [ 2, 4, 0 ]
|> Tree.Path.step
--> Just ( 2, Tree.Path.follow [ 4, 0 ] )
Tree.Path.atTrunk
|> Tree.Path.step
--> Nothing
-- in this package as Tree.Navigate.to
at path =
case Tree.Path.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.
Tree.Path.follow [ 1, 2, 3 ]
|> Tree.Path.toParent
--> Just (Tree.Path.follow [ 1, 2 ])
Tree.Path.atTrunk
|> Tree.Path.toParent
--> NothingThe path to its ...th child from there.
Tree.Path.atTrunk
|> Tree.Path.toChild 4
|> Tree.Path.toChild 1
|> Tree.Path.toChild 8
--> Tree.Path.follow [ 4, 1, 8 ]Example ↓ is for illustration only.
You'll have a much easier time using Tree.Navigate.restructure.
view path tree =
button [ onPress (Clicked path) ]
:: (Tree.children tree
|> List.indexedMap
(\index ->
view (path |> Tree.Path.toChild index)
)
)
|> column [ padding (10 / Tree.Path.depth path) ]
The unique location of a (child-)
Treein aTree.You describe which branch index to go along every step.
Represented as a tuple to simplify pattern matching and enable using it as a
comparablekey.Instead of working with that
Listdirectly, I recommend using the helpers here to create and operate on these paths.