Init Zipper from NonEmpty list type.
fromNonEmpty ( 1, [ 2, 3 ] )
|> current
--> 1
fromNonEmpty ( 1, [ 2, 3 ] )
|> toList
--> [ 1, 2, 3 ]Init Zipper from List.
This operation is not sucessfull for []
fromList []
--> Nothing
fromList [1, 2, 3]
--> Just (custom [] 1 [2,3])Init Zipper by consing value onto the list.
fromCons 1 [ 2, 3 ]
|> current
--> 1Init Zipper by consing List onto NonEmpty.
The head of NonEmpty stays in focus while list is a list of previous heads.
fromConsList [] (1, [2])
|> current
--> 1
fromConsList [1, 2] (3, [4])
|> prev
|> Maybe.map current
--> Just 2Init Zipper from parts.
custom [1,2] 3 [4,5]
|> current
--> 3
custom [1,2] 3 [4,5]
|> prev
|> Maybe.map current
--> Just 2Functions that query Zipper for additional data.
Get List of all values preceding current focus.
custom [1,2] 3 [4,5]
|> listPrev
--> [1,2]Get List of all values following current focus.
custom [1,2] 3 [4,5]
|> listNext
--> [4,5]Check if there is next value before current focus.
custom [1,2] 3 [4,5]
|> hasPrev
--> True
custom [] 1 [2,3]
|> hasPrev
--> FalseCheck if there is next value after current focus.
custom [1,2] 3 [4,5]
|> hasNext
--> True
custom [1,2] 3 []
|> hasNext
--> FalseFollowing function inserts new value into existing Zipper.
This function insert value around focus without moving it.
Insert new value before current focus.
fromConsList [1, 2] (4, [5])
|> insertBefore 3
|> toList
--> [1,2,3,4,5]
fromConsList [1, 2] (4, [5])
|> insertBefore 3
|> current
--> 4Insert new value after current focus.
fromConsList [1, 2] (3, [5])
|> insertAfter 4
|> toList
--> [1,2,3,4,5]
fromConsList [1, 2] (3, [5])
|> insertAfter 4
|> current
--> 3These functions insert value around focus while moving focus on newly inserted value.
Insert value before current focus and move focus to it.
fromConsList [1, 2] (4, [5])
|> consBefore 3
|> toList
--> [1,2,3,4,5]
fromConsList [1, 2] (4, [5])
|> consBefore 3
|> current
--> 3Insert new value after current focus and move focus to it.
fromConsList [1, 2] (3, [5])
|> consAfter 4
|> toList
--> [1,2,3,4,5]
fromConsList [1, 2] (3, [5])
|> consAfter 4
|> current
--> 4Functions that move focus within Zipper around without losing data.
These function will return Nothing when moving out of bounds of Zipper.
Move focus to next value.
custom [] 1 [2,3]
|> next
|> Maybe.map current
|> Just 2
custom [] 1 []
|> next
--> NothingMove focus to next value.
custom [1, 2] 3 []
|> prev
|> Maybe.map current
|> Just 2
custom [] 1 []
|> prev
--> NothingThese function will move in direction but won't reach out of bound. When end on any side is reached, the last value on this side is returned.
Move focus to next value if such value exists.
custom [] 1 [2,3]
|> attemptNext
|> current
|> 2
custom [] 1 []
|> attemptNext
|> current
--> 1Move focus to previous value if such value exists.
custom [1] 2 [3]
|> attemptPrev
|> current
|> 1
custom [] 1 []
|> attemptPrev
|> current
--> 1Perform attemptPrev n times.
Perform attemptNext n times.
These helper function will move from either side of a Zipper
Move focus to the very first value
custom [ 1, 2, 3 ] 4 [ 5, 6, 7 ]
|> start
|> current
--> 1Move focus to the very last value
custom [ 1, 2, 3 ] 4 [ 5, 6, 7 ]
|> end
|> current
--> 7These function move in cycles around the zipper. Value on a very start is preceeded by
value in the end. These function simply move in circle and never reach the end of a Zipper.
Move focus to next value, go back to fist value if current value is last.
custom [] 1 [2,3]
|> forward
|> current
|> 2
custom [1,2] 3 []
|> forward
|> current
--> 1Move focus to previous value, go to last value if current value is fist one.
custom [1, 2] 3 []
|> backward
|> current
|> 2
custom [] 1 [2,3]
|> backward
|> current
--> 3Move [backward]{#backward} n times.
Map a function over Zipper
map String.fromInt (custom [1] 2 [3, 4])
|> toList
--> ["1", "2", "3", "4"]Indexed map relative to the position in the zipper.
custom ["a", "b"] "c" ["d"]
|> relativeIndexedMap (\index el -> (index, el))
|> toList
--> [(-2,"a"),(-1,"b"), (0,"c"), (1,"d")]Indexed map. Starting with 0 from the beginning of the zipper
custom ["a", "b"] "c" ["d"]
|> absoluteIndexedMap (\index el -> (index, el))
|> toList
--> [(0,"a"),(1,"b"), (2,"c"), (3,"d")]Recude Zipper from left
foldl (+) 0 <| custom [1,2] 3 [4]
--> 10Reduce Zipper from right
foldr (+) 0 <| custom [1,2] 3 [4]
--> 10Collapse Zipper a into a value from left
foldl1 (++) <| custom ["hello"] " " ["world"]
--> "world hello"Collapse Zipper a into a value from right
foldr1 (+) (custom [1,2] 3 [4])
--> 10
foldr1 (++) (custom ["hello"] " " ["world"])
--> "hello world"Combine two Zippers with a given function. In case where one of the two zippers is longer the extra elements are ignored
map2 (+) (custom [1] 2 []) (custom [1] 1 [])
|> toList
--> [2, 3]
map2 (+) (custom [1] 2 [3]) (custom [1] 1 [])
|> toList
--> [2, 3]Map over multiple Zippers.
map (+) (custom [1] 2 [3])
|> andMap (custom [1] 2 [3])
|> toList
--> [2, 4, 6]Create Zipper containing all possible variants of given Zipper.
Current version is focused one.
custom [1] 2 [3]
|> duplicate
|> current
--> custom [1] 2 [3]
custom [1] 2 [3]
|> duplicate
|> forward
|> current
--> custom [1, 2] 3 []Map value to a new value based on surrounding structure.
This is a more advanced function following Comonad
-- negate all True values which next value is not True itself
fromNonEmpty ( True, [ True, True, False, True, True ] )
|> extend (\zipper ->
let prev = current <| backward zipper
in prev && current zipper
)
|> toNonEmpty
--> (True, [True, True, False, False, True])duplicate and covert to List.
This function might be useful in view code.
Convert Zipper back to NonEmpty.
This function won't loose data, all previous heads are added back.
fromCons 1 [2,3]
|> toNonEmpty
--> (1, [2, 3])
fromConsList [1,2] (3, [4])
|> toNonEmpty
--> (1, [2,3,4])
Zipper type.
This can be thought of as
NonEmptywhich holds keeps track of unconsed data.Unlike
NonEmptythis type is opaque as it needs to ensure internal invariants.