Zipper implementation for Lazy.Tree.
A zipper is a technique of representing an aggregate data structure so that it is convenient for writing programs that traverse the structure arbitrarily and update its contents, especially in purely functional programming languages.
Zipper is a secret sauce that gives Tree real power.
It provides an easy way to query and modify the Tree in a clever and very flexible way.
Types within this module are exposed type aliases to make it easy to extend the default functionality of Zipper.
Init Zipper for Tree.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> current
--> "foo"Get the root item of the current Tree.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> current
--> "foo"Get children of current Tree.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> children
--> [ "bar" ]Check if Zipper is focused on the root Tree.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> isRoot
--> TrueCheck if current Tree in Zipper is empty.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> isEmpty
--> True
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> isEmpty
--> FalseAttempt to perform action over zipper and return original Zipper in cases where this action returns Nothing.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> attempt delete
|> current
--> "foo"
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> attempt (open ((==) "foo"))
|> attempt delete
|> current
--> "foo"Extract current Tree from a Zipper.
useful in case where you don't want to use pattern matching
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> getTree
|> T.item
--> "foo"Insert sub Tree into current Tree in Zipper.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> insert (T.singleton "baz")
|> children
--> [ "bar", "baz" ]Delete current Tree from Zipper.
Returns Nothing if root node is removed.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> delete
--> Nothing
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> open (always True)
|> Maybe.andThen delete
|> Maybe.map current
--> Just "foo"Update current Tree using given function.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> update (T.map (\a -> a ++ " fighter"))
|> current
--> "foo fighter"Update the root item of the current Tree using given function.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> updateItem (\i -> i ++ " fighter")
|> current
--> "foo fighter"Replace current Tree with new one.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> setTree (T.singleton "bar")
|> current
--> "bar"Open the first child that satisfies given condition. If there is no child satisfying the condition, returns Nothing.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> open ((==) "bar")
|> Maybe.map current
--> Just "bar"
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz") )
|> attempt (open ((==) "bar"))
|> attempt (open ((==) "baz"))
|> current
--> "baz"
T.singleton "foo"
|> fromTree
|> open (always True)
--> NothingUse given function to convert current breadcrumb path to a list
Resulting list of breadcrumbs contains currently focused item as well.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> attemptOpenPath (==) [ "bar" ]
|> getPath identity
--> [ "foo", "bar" ]
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz") )
|> attemptOpenPath (==) [ "bar", "baz" ]
|> getPath identity
--> [ "foo", "bar", "baz" ]Open multiple levels reducing list by given function.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz") )
|> openPath (==) [ "bar", "baz" ]
|> Result.map current
--> Ok "baz"
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> openPath (==) [ "not-here", "baz" ]
|> Result.map current
--> Err "Can't resolve open"Get List of Zippers for all children of current Zipper
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> insert (T.singleton "baz")
|> openAll
|> List.map current
--> [ "bar", "baz" ]Similar to openPath but ignore failed steps.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> attemptOpenPath (==) [ "not-here", "bar" ]
|> current
--> "bar"
T.singleton "foo"
|> fromTree
|> attemptOpenPath (==) [ "baz" ]
|> current
--> "foo"
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz"))
|> attemptOpenPath (==) [ "not-here", "bar", "missing", "baz" ]
|> current
--> "baz"Return back to parent of current Tree in given Zipper.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> open ((==) "bar")
|> Maybe.andThen up
|> Maybe.map current
--> Just "foo"
T.singleton "baz"
|> fromTree
|> up
--> NothingPerform up n times.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz") )
|> open ((==) "bar")
|> Maybe.andThen (open ((==) "baz"))
|> Maybe.andThen (upwards 2)
|> Maybe.map current
--> Just "foo"Returns given Zipper return if 0 is passed:
T.singleton "foo"
|> fromTree
|> upwards 0
|> Maybe.map current
--> Just "foo"Return Nothing if there are not enough ancestors in Zipper:
T.singleton 4
|> fromTree
|> upwards 1
--> NothingReturn Nothing if negative integer is passed:
T.singleton 4
|> fromTree
|> upwards -1
--> NothingNavigate back to the root Tree.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz") )
|> open ((==) "bar")
|> Maybe.andThen (open ((==) "baz"))
|> Maybe.map root
|> Maybe.map current
--> Just "foo"
T.singleton "foo"
|> fromTree
|> root
|> current
--> "foo"Map function over Zipper.
import Lazy.Tree as T
T.singleton 1
|> fromTree
|> map ((+) 1)
|> current
--> 2Performs filter on current Tree in Zipper. See Tree.filter for more information.
import Lazy.LList as LL
import Lazy.Tree as T
T.Tree 1 (LL.fromList [ T.singleton 2, T.singleton 3, T.singleton 4 ])
|> fromTree
|> filter ((>) 4)
|> children
--> [ 2, 3 ]
T.Tree 1 (LL.fromList [ T.singleton 2, T.singleton 3, T.singleton 4 ])
|> fromTree
|> attempt (open ((==) 1))
|> filter ((<) 2)
|> root
|> children
--> [ 3, 4 ]
T.Tree 1 (LL.fromList [ T.insert (T.singleton 5) <| T.singleton 2, T.insert (T.singleton 6) <| T.singleton 3, T.singleton 4 ])
|> fromTree
|> attempt (open ((==) 1))
|> filter ((<) 2)
|> getTree
|> T.descendants
|> LL.andThen (LL.map T.item << T.descendants)
|> LL.toList
--> [ 6 ]Get List of Breadcrumbs .
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz"))
|> attemptOpenPath (==) [ "bar", "baz" ]
|> breadcrumbs
--> [ "bar", "foo" ]Get Breadcrumbs as indexed List.
import Lazy.Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz"))
|> attemptOpenPath (==) [ "bar", "baz" ]
|> indexedBreadcrumbs
--> [ ( 1, "bar" ), ( 2, "foo" )]
** Be careful when comparing
Breadcrumbs using(==).** Due to use of laziness(==)isn't reliable for comparing Breadcrumbs.Breadcrumbs are private type not meant to be manipulated directly. However it's possible to extract breadcrumbs from
Zipperin transformed format usingbreadcrumbsandindexedBreadcrumbsfunctions which are meant for public use.