MackeyRMS / elm-accessors / Tree.Accessors

List.Accessors

at : Tree.Path.TreePath -> Base.Traversal (Tree a) a x y

This accessor combinator lets you access the label at a particular TreePath.

import Accessors exposing (..)
import Tree exposing (Tree, tree)
import Tree.Accessors as Tree
import Lens as L

leaf : a -> Tree a
leaf a =
    tree a []

simpleTree : Tree String
simpleTree =
    tree "root"
        [ tree "a" [ leaf "b"]
        , tree "x" [ leaf "y"]
        ]

try (Tree.at [0]) simpleTree
--> Just "a"

map (Tree.at [0]) String.toUpper simpleTree
--> tree "root" [ tree "A" [ leaf "b" ] , tree "x" [ leaf "y" ] ]

label_ : Base.Lens ls (Tree a) a x y

This accessor combinator lets you access the label of a given Tree.

import Accessors exposing (..)
import Tree exposing (Tree, tree)
import Tree.Accessors as Tree
import Lens as L

leaf : a -> Tree a
leaf a =
    tree a []

simpleTree : Tree String
simpleTree =
    tree "root"
        [ tree "a" [ leaf "b"]
        , tree "x" [ leaf "y"]
        ]

get (Tree.label_) simpleTree
--> "root"

set (Tree.label_) "top 'o the mornin' to yah!" simpleTree
--> tree "top 'o the mornin' to yah!" [ tree "a" [ leaf "b" ] , tree "x" [ leaf "y" ] ]

map (Tree.label_) String.toUpper simpleTree
--> tree "ROOT" [ tree "a" [ leaf "b" ] , tree "x" [ leaf "y" ] ]

path : Tree.Path.TreePath -> Base.Traversal (Tree a) (Tree a) x y

This accessor combinator lets you access a sub-tree at a given TreePath.

import Accessors exposing (..)
import Tree exposing (Tree, tree)
import Tree.Accessors as Tree
import Lens as L

leaf : a -> Tree a
leaf a =
    tree a []


simpleTree : Tree String
simpleTree =
    tree "root"
        [ tree "a" [ leaf "b"]
        , tree "x" [ leaf "y"]
        ]

try (Tree.path [0,0]) simpleTree
--> Just (leaf "b")

set (Tree.path [1,0]) (tree "hey!" [leaf "deeper"]) simpleTree
--> tree "root" [ tree "a" [ leaf "b" ] , tree "x" [ tree "hey!" [leaf "deeper"] ] ]

map (Tree.path [0]) (Tree.updateLabel (\s -> "gimme an " ++ String.toUpper s ++ "!")) simpleTree
--> tree "root" [ tree "gimme an A!" [ leaf "b" ] , tree "x" [ leaf "y" ] ]

at_ : (a -> String) -> a -> Base.Traversal (Tree { z | id : a }) (Tree { z | id : a }) x y

id : String -> Base.Traversal (Tree { z | id : String }) (Tree { z | id : String }) x y

each : Base.Traversal_ (Tree a) (Tree b) a b x y

This accessor combinator lets you modify the type of each label of a Tree.

import Accessors exposing (..)
import Tree exposing (Tree, tree)
import Tree.Accessors as Tree
import Lens as L

leaf : a -> Tree a
leaf a =
    tree a []

simpleTree : Tree String
simpleTree =
    tree "r"
        [ tree "a" [ leaf "b"]
        , tree "x" [ leaf "y"]
        ]

all Tree.each simpleTree
--> ["r", "a", "b", "x", "y"]

set Tree.each "1" simpleTree
--> tree "1" [ tree "1" [ leaf "1" ] , tree "1" [ leaf "1" ] ]

map Tree.each (\s -> "gimme an " ++ String.toUpper s ++ "!") simpleTree
--> tree "gimme an R!" [ tree "gimme an A!" [ leaf "gimme an B!" ] , tree "gimme an X!" [ leaf "gimme an Y!" ] ]