Zipper implementation.
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 extend default functionality of Zipper.
Init Zipper for Tree.
import Tree as T
T.singleton "foo"
|> fromTree
|> current
--> "foo"Get item of current Tree.
import Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> current
--> "foo"Get children of current Tree.
import Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar")
|> children
--> [ "bar" ]Check if Zipper is focused on root Tree.
import Tree as T
T.singleton "foo"
|> fromTree
|> isRoot
--> TrueCheck if current Tree in Zipper is empty.
import 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 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 mathcing
import Tree as T
T.singleton "foo"
|> fromTree
|> getTree
|> T.item
--> "foo"Insert sub Tree into current Tree in Zipper.
import 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 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 Tree as T
T.singleton "foo"
|> fromTree
|> update (T.map (\a -> a ++ " fighter"))
|> current
--> "foo fighter"Update item of current Tree using given function.
import Tree as T
T.singleton "foo"
|> fromTree
|> updateItem (\i -> i ++ " fighter")
|> current
--> "foo fighter"Replace current Tree with new one.
import Tree as T
T.singleton "foo"
|> fromTree
|> setTree (T.singleton "bar")
|> current
--> "bar"Open first children that satisfy given condition.
import 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 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 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 for \"not-here\""Get List of Zippers for all children of current Zipper
import 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 ingnore failed steps.
import 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", "missng", "baz" ]
|> current
--> "baz"Return back to parent of current Tree in given Zipper.
import 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 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
--> NothingBack to root Tree.
import 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 Tree as T
T.singleton 1
|> fromTree
|> map ((+) 1)
|> current
--> 2Performs filter on current Tree in Zipper. See Tree.filter for more informations.
import Tree as T
T.Tree 1 [ T.singleton 2, T.singleton 3, T.singleton 4 ]
|> fromTree
|> filter ((>) 4)
|> children
--> [ 2, 3 ]
T.Tree 1 [ T.singleton 2, T.singleton 3, T.singleton 4 ]
|> fromTree
|> attempt (open ((==) 1))
|> filter ((<) 2)
|> root
|> children
--> [ 3, 4 ]
T.Tree 1 [ 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
|> List.andThen (List.map T.item << T.descendants)
--> [ 6 ]Get List of Breacrubs .
import Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz"))
|> attemptOpenPath (==) [ "bar", "baz" ]
|> breadcrumbs
--> [ "bar", "foo" ]Get Breacrubs as indexed List.
import Tree as T
T.singleton "foo"
|> fromTree
|> insert (T.singleton "bar" |> T.insert (T.singleton "baz"))
|> attemptOpenPath (==) [ "bar", "baz" ]
|> indexedBreadcrumbs
--> [ ( 1, "bar" ), ( 2, "foo" )]
Breadcrumbs are private type not meant to be manipulated directly. However it's possible to extract breadcrubs from
Zipperin transformed format usingbreadcrumbsandindexedBreadcrumbsfunctions which are meant for public use.