lue-bird/elm-rosetree-path/1.0.0

NOTE: the latest version is 2.0.5

Tree.Extra.Lue

To import as Tree exposing (leaf).

leaf : a -> Tree a

Alias to Tree.singleton: A Tree without children. It can be exposed → less noise.

import Tree.Extra.Lue as Tree exposing (leaf)


leaf 5 |> Tree.label
--> 5


leaf "foo" |> Tree.children
--> []

scan

at : TreePath -> Tree a -> Maybe (Tree a)

Following the TreePath, where do we end?

leaf "jo"
    |> Tree.at TreePath.atTrunk
--> Just (leaf "jo")


tree "jo"
    [ leaf "ann"
    , tree "mic"
        [ leaf "igg"
        , leaf "dee"
        , leaf "bee"
        ]
    ]
    |> Tree.at (TreePath.go [ 1, 2 ])
--> Just (leaf "bee")


tree "jo"
    [ leaf "ann"
    , tree "mic"
        [ leaf "igg"
        , leaf "dee"
        ]
    ]
    |> Tree.at (TreePath.go [ 1, 2 ])
--> Nothing

modify

updateAt : TreePath -> (Tree a -> Tree a) -> Tree a -> Tree a

Change the sub-tree at a TreePath based on its current value.

tree "jo"
    [ leaf "ann"
    , tree "mic"
        [ leaf "igg"
        , leaf "dee"
        , leaf "bee"
        ]
    ]
    |> Tree.updateAt (TreePath.go [ 1, 2 ])
        (Tree.mapLabel String.toUpper)
--> tree "jo"
-->     [ leaf "ann"
-->     , tree "mic"
-->         [ leaf "igg"
-->         , leaf "dee"
-->         , leaf "BEE"
-->         ]
-->     ]


tree "jo"
    [ leaf "ann"
    , tree "mic"
        [ leaf "igg"
        , leaf "dee"
        ]
    ]
    |> Tree.updateAt (TreePath.go [ 1, 2 ])
        (Tree.mapLabel String.toUpper)
--> tree "jo"
-->     [ leaf "ann"
-->     , tree "mic"
-->         [ leaf "igg"
-->         , leaf "dee"
-->         ]
-->     ]
removeAt : TreePath -> List (Tree a) -> List (Tree a)

Remove the sub-Tree at the end of a TreePath from a Tree.

[ leaf "ann"
, tree "mic"
    [ leaf "igg"
    , leaf "dee"
    , leaf "bee"
    ]
]
|> Tree.removeAt (TreePath.go [ 1, 2 ])
--> [ leaf "ann"
--> , tree "mic"
-->     [ leaf "igg"
-->     , leaf "dee"
-->     ]
--> ]




[ leaf "ann"
, tree "mic"
    [ leaf "igg"
    , leaf "dee"
    ]
]
|> Tree.removeAt (TreePath.go [ 1, 2 ])
--> [ leaf "ann"
--> , tree "mic"
-->     [ leaf "igg"
-->     , leaf "dee"
-->     ]
--> ]


[ leaf "jo" ]
    |> Tree.removeAt TreePath.atTrunk
--> []
replaceAt : TreePath -> Tree a -> Tree a -> Tree a

Replace the sub-tree at a TreePath with a new Tree.

tree "jo"
    [ leaf "ann"
    , tree "mic"
        [ leaf "igg"
        , leaf "dee"
        , leaf "bee"
        ]
    ]
    |> Tree.replaceAt (TreePath.go [ 1, 2 ])
        (leaf "be")
--> tree "jo"
-->     [ leaf "ann"
-->     , tree "mic"
-->         [ leaf "igg"
-->         , leaf "dee"
-->         , leaf "be"
-->         ]
-->     ]


tree "jo"
    [ leaf "ann"
    , tree "mic"
        [ leaf "igg"
        , leaf "dee"
        ]
    ]
    |> Tree.replaceAt (TreePath.go [ 1, 2 ])
        (leaf "be")
--> tree "jo"
-->     [ leaf "ann"
-->     , tree "mic"
-->         [ leaf "igg"
-->         , leaf "dee"
-->         ]
-->     ]
appendChildren : List (Tree a) -> Tree a -> Tree a

Put a list of children after all current children.

tree "hello" [ leaf "you" ]
    |> Tree.appendChildren
        [ leaf "and", leaf "you" ]
--> tree "hello"
-->     [ leaf "you", leaf "and", leaf "you" ]
prependChildren : List (Tree a) -> Tree a -> Tree a

Put a list of children left to all current children.

tree "dear" [ leaf "George" ]
    |> Tree.prependChildren
        [ leaf "May", leaf "and" ]
--> tree "dear"
-->     [ leaf "May", leaf "and", leaf "George" ]

filter

when : (Tree a -> Bool) -> List (Tree a) -> List (Tree a)

Take all the branches where condition is met. When a branch fails, all its children are checked.

tree { name = "boss tree", selected = Unselected }
    [ tree { name = "hugg", selected = Selected }
        [ leaf { name = "zugg", selected = Selected }
        , leaf { name = "gugg", selected = Unselected }
        ]
    , leaf { name = "naomi", selected = Unselected }
    , tree { name = "unselmo", selected = Unselected }
        [ leaf { name = "selmo", selected = Selected }
        , leaf { name = "helmo", selected = Unselected }
        ]
    ]
    |> Tree.children
    |> Tree.when
        (Tree.label
            >> .selected
            >> (==) Selected
        )


--> [ tree { name = "hugg", selected = Selected }
-->     [ leaf { name = "zugg", selected = Selected }
-->     , leaf { name = "gugg", selected = Unselected }
-->     ]
--> , leaf { name = "selmo", selected = Selected }
--> ]

transform

mapWithPath : (TreePath -> a -> b) -> Tree a -> Tree b

Alter every label based on its TreePath and current value.

tree 1
    [ leaf 2
    , tree 3 [ leaf 4 ]
    , leaf 5
    ]
    |> Tree.mapWithPath (\path n -> ( n * 2, path ))
--> tree ( 2, TreePath.atTrunk )
-->     [ leaf ( 4, TreePath.go [ 0 ] )
-->     , tree ( 6, TreePath.go [ 1 ] )
-->         [ leaf ( 8, TreePath.go [ 1, 0 ] ) ]
-->     , leaf ( 10, TreePath.go [ 2 ] )
-->     ]
serialize : Codec error a -> Codec error (Tree a)

A Codec to serialize a Tree.