Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Zipper.map #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 3 additions & 115 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"elm": "^0.19.0",
"elm": "^0.19.0-no-deps",
"elm-format": "^0.8.0",
"elm-test": "^0.19.0-rev3",
"elm-verify-examples": "^3.0.1",
Expand Down
61 changes: 59 additions & 2 deletions src/Tree/Zipper.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Tree.Zipper exposing
( Zipper, fromTree, fromForest, toTree, toForest, tree, label, children
, firstChild, lastChild, parent, forward, backward, root, lastDescendant, nextSibling, previousSibling
, siblingsBeforeFocus, siblingsAfterFocus
, mapTree, replaceTree, removeTree, mapLabel, replaceLabel, append, prepend
, map, mapTree, replaceTree, removeTree, mapLabel, replaceLabel, append, prepend
, findNext, findPrevious, findFromRoot
)

Expand All @@ -29,7 +29,7 @@ modify the tree structure while walking through it.

# Modification

@docs mapTree, replaceTree, removeTree, mapLabel, replaceLabel, append, prepend
@docs map, mapTree, replaceTree, removeTree, mapLabel, replaceLabel, append, prepend


# Utility
Expand Down Expand Up @@ -805,6 +805,63 @@ removeTree (Zipper zipper) =
}


{-| Just like `Tree.map` but preserves focus

import Tree exposing (Tree)
import Tree.Zipper as Zipper exposing (Zipper)

myTree : Tree Int
myTree =
Tree.tree 0
[ Tree.tree 1
[ Tree.singleton 2
, Tree.singleton 3
]
, Tree.tree 4
[ Tree.singleton 5 ]
]

myZipper : Maybe (Zipper Int)
myZipper =
myTree
|> fromTree
|> findNext ((==) 4)
|> Maybe.map (map ((+) 1))

myZipper
|> Maybe.map label
--> Just 5

myZipper
|> Maybe.andThen firstChild
|> Maybe.map label
--> Just 6


myZipper
|> Maybe.andThen parent
|> Maybe.map label
--> Just 1

-}
map : (a -> b) -> Zipper a -> Zipper b
map f (Zipper { focus, before, after, crumbs }) =
Zipper
{ focus = Tree.map f focus
, before = List.map (Tree.map f) before
, after = List.map (Tree.map f) after
, crumbs = List.map (mapCrumb f) crumbs
}


mapCrumb : (a -> b) -> Crumb a -> Crumb b
mapCrumb f crumb =
{ label = f crumb.label
, before = List.map (Tree.map f) crumb.before
, after = List.map (Tree.map f) crumb.after
}


{-| Map a function on the label of the currently focused tree.
-}
mapLabel : (a -> a) -> Zipper a -> Zipper a
Expand Down