lue-bird/elm-rosetree-path/1.1.0

NOTE: the latest version is 2.0.5

TreePath

import exposing (TreePath, atTrunk).

type alias TreePath =
List Int

The unique location of a child-Tree in a Tree.

You describe which branch index to go along every step.

[ {-first go to its child at index-}0
, {-then go to the child of its child at index-}2
, ...
]
atTrunk : TreePath

Stay at that tree. Don't go anywhere.

TreePath.atTrunk
--> TreePath.go []


TreePath.atTrunk
    |> TreePath.depth
--> 0
go : List Int -> TreePath

Conveniently describe the route to go from the trunk.

TreePath.go [ 2, 4, 0 ]
--> [ 2, 4, 0 ]


-- or
TreePath.go [ 2, 4, 0 ]
--> TreePath.atTrunk
-->     |> TreePath.toChild 2
-->     |> TreePath.toChild 4
-->     |> TreePath.toChild 0

scan

depth : TreePath -> Int

On 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
--> 3
goesToChildOf : TreePath -> TreePath -> Bool

Does 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 ])
--> False
goesToParentOf : TreePath -> TreePath -> Bool

Does 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 ])
--> False

modify

step : TreePath -> Maybe ( Int, TreePath )

Go to the next child index.

import Tree exposing (Tree)
import TreePath exposing (TreePath)




TreePath.go [ 2, 4, 0 ]
    |> TreePath.step
--> Just ( 2, TreePath.go [ 4, 0 ] )


TreePath.atTrunk
    |> TreePath.step
--> Nothing


at path =
    case TreePath.step path of
        Nothing ->
            Just


        Just ( index, further ) ->
            Tree.children
                >> List.getAt index
                >> Maybe.andThen (at further)
toParent : TreePath -> Maybe TreePath

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
--> Nothing
toChild : Int -> TreePath -> TreePath

The path to its ...th child from there.

TreePath.atTrunk
    |> TreePath.toChild 4
    |> TreePath.toChild 1
    |> TreePath.toChild 8
--> TreePath.go [ 4, 1, 8 ]
  1. Take the branch at index 4
  2. Then take its branch at index 1
  3. Its child at index 8 is where you wanted to go

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) ]

transform

serialize : Codec serializeError_ TreePath

A Codec to serialize a Tree.