lue-bird / elm-rosetree-path / Forest.Navigate

Additional helpers for a list of zwilias/elm-rosetrees using ForestPath


type alias Forest label =
List (Tree label)

A List of zwilias/elm-rosetrees

Example use cases

observe

to : Forest.Path.ForestPath -> Forest label -> Maybe (Tree label)

Following the ForestPath, where do we end?

import Tree exposing (tree)
import Forest.Navigate
import Tree.Path
import Forest.Path

[ Tree.singleton "gee"
, tree "jo"
    [ Tree.singleton "ann"
    , tree "mic"
        [ Tree.singleton "igg"
        , Tree.singleton "dee"
        , Tree.singleton "bee"
        ]
    ]
]
    |> Forest.Navigate.to (Forest.Path.fromIndex 1 (Tree.Path.follow [ 1, 2 ]))
--> Just (Tree.singleton "bee")

[ Tree.singleton "gee"
, tree "jo"
    [ Tree.singleton "ann"
    , tree "mic"
        [ Tree.singleton "igg"
        , Tree.singleton "dee"
        ]
    ]
]
    |> Forest.Navigate.to (Forest.Path.fromIndex 1 (Tree.Path.follow [ 1, 2 ]))
--> Nothing

altering

alter : Forest.Path.ForestPath -> (Tree label -> Tree label) -> Forest label -> Forest label

Change its sub-tree at a given ForestPath based on its current value.

import Tree exposing (tree)
import Forest.Navigate
import Tree.Path
import Forest.Path

[ Tree.singleton "gee"
, tree "jo"
    [ Tree.singleton "ann"
    , tree "mic"
        [ Tree.singleton "igg"
        , Tree.singleton "dee"
        , Tree.singleton "bee"
        ]
    ]
]
    |> Forest.Navigate.alter (Forest.Path.fromIndex 1 (Tree.Path.follow [ 1, 2 ]))
        (Tree.mapLabel String.toUpper)
--> [ Tree.singleton "gee"
--> , tree "jo"
-->     [ Tree.singleton "ann"
-->     , tree "mic"
-->         [ Tree.singleton "igg"
-->         , Tree.singleton "dee"
-->         , Tree.singleton "BEE"
-->         ]
-->     ]
--> ]

[ Tree.singleton "gee"
, tree "jo"
    [ Tree.singleton "ann"
    , tree "mic"
        [ Tree.singleton "igg"
        , Tree.singleton "dee"
        ]
    ]
]
    |> Forest.Navigate.alter (Forest.Path.fromIndex 1 (Tree.Path.follow [ 1, 2 ]))
        (Tree.mapLabel String.toUpper)
--> [ Tree.singleton "gee"
--> , tree "jo"
-->     [ Tree.singleton "ann"
-->     , tree "mic"
-->         [ Tree.singleton "igg"
-->         , Tree.singleton "dee"
-->         ]
-->     ]
--> ]

remove : Forest.Path.ForestPath -> Forest label -> Forest label

Remove the sub-Tree at the end of a given ForestPath and its children.

import Tree exposing (tree)
import Forest.Navigate
import Tree.Path
import Forest.Path

[ Tree.singleton "ann"
, tree "mic"
    [ Tree.singleton "igg"
    , Tree.singleton "dee"
    , Tree.singleton "bee"
    ]
]
    |> Forest.Navigate.remove (Forest.Path.fromIndex 1 (Tree.Path.follow [ 2 ]))
--> [ Tree.singleton "ann"
--> , tree "mic"
-->     [ Tree.singleton "igg"
-->     , Tree.singleton "dee"
-->     ]
--> ]


[ Tree.singleton "ann"
, tree "mic"
    [ Tree.singleton "igg"
    , Tree.singleton "dee"
    ]
]
    |> Forest.Navigate.remove (Forest.Path.fromIndex 1 (Tree.Path.follow [ 2 ]))
--> [ Tree.singleton "ann"
--> , tree "mic"
-->     [ Tree.singleton "igg"
-->     , Tree.singleton "dee"
-->     ]
--> ]

map : ({ label : label, path : Forest.Path.ForestPath } -> mappedLabel) -> Forest label -> Forest mappedLabel

Change every tree label based on its ForestPath and current value.

import Tree exposing (tree)
import Forest.Navigate
import Forest.Path
import Tree.Path

[ Tree.singleton -1
, tree 1
    [ Tree.singleton 2
    , tree 3 [ Tree.singleton 4 ]
    , Tree.singleton 5
    ]
]
    |> Forest.Navigate.map
        (\n -> ( n.label * 2, n.path ))
--> [ Tree.singleton ( -2, Forest.Path.fromIndex 0 Tree.Path.atTrunk )
--> , tree ( 2, Forest.Path.fromIndex 1 Tree.Path.atTrunk )
-->     [ Tree.singleton ( 4, Forest.Path.fromIndex 1 (Tree.Path.follow [ 0 ]) )
-->     , tree ( 6, Forest.Path.fromIndex 1 (Tree.Path.follow [ 1 ]) )
-->         [ Tree.singleton ( 8, Forest.Path.fromIndex 1 (Tree.Path.follow [ 1, 0 ]) ) ]
-->     , Tree.singleton ( 10, Forest.Path.fromIndex 1 (Tree.Path.follow [ 2 ]) )
-->     ]
--> ]