turboMaCk/non-empty-list-alias/1.0.0

NOTE: the latest version is 1.4.0

List.NonEmpty.Zipper

type Zipper a

Zipper type.

This can be thought of as NonEmpty which holds keeps track of unconsed data.

Unlike NonEmpty this type is opaque as it needs to ensure internal invariants.

singleton : a -> Zipper a

Put single value into a Zipper.

singleton "foo"
|> current
--> "foo"
fromNonEmpty : NonEmpty a -> Zipper a

Init Zipper from NonEmpty list type.

fromNonEmpty ( 1, [ 2, 3 ] )
|> current
--> 1


fromNonEmpty ( 1, [ 2, 3 ] )
|> toList
--> [ 1, 2, 3 ]
fromList : List a -> Maybe (Zipper a)

Init Zipper from List. This operation is not sucessfull for []

fromList []
--> Nothing


fromList [1, 2, 3]
--> Just (custom [] 1 [2,3])
fromCons : a -> List a -> Zipper a

Init Zipper by consing value onto the list.

fromCons 1 [ 2, 3 ]
|> current
--> 1
fromConsList : List a -> NonEmpty a -> Zipper a

Init 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 2
custom : List a -> a -> List a -> Zipper a

Init Zipper from parts.

custom [1,2] 3 [4,5]
|> current
--> 3


custom [1,2] 3 [4,5]
|> prev
|> Maybe.map current
--> Just 2

Query

Functions that query Zipper for additional data.

current : Zipper a -> a

Get current focus

custom [1,2] 3 [4,5]
|> current
--> 3
listPrev : Zipper a -> List a

Get List of all values preceding current focus.

custom [1,2] 3 [4,5]
|> listPrev
--> [1,2]
listNext : Zipper a -> List a

Get List of all values following current focus.

custom [1,2] 3 [4,5]
|> listNext
--> [4,5]
hasPrev : Zipper a -> Bool

Check if there is next value before current focus.

custom [1,2] 3 [4,5]
|> hasPrev
--> True


custom [] 1 [2,3]
|> hasPrev
--> False
hasNext : Zipper a -> Bool

Check if there is next value after current focus.

custom [1,2] 3 [4,5]
|> hasNext
--> True


custom [1,2] 3 []
|> hasNext
--> False
length : Zipper a -> Int

Get length of Zipper

custom [1,2] 3 [4]
|> length
--> 4

Insert new values

Following function inserts new value into existing Zipper.

Insert without chaning focus

This function insert value around focus without moving it.

insertBefore : a -> Zipper a -> Zipper a

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
--> 4
insertAfter : a -> Zipper a -> Zipper a

Insert 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
--> 3

Insert and change focus

These functions insert value around focus while moving focus on newly inserted value.

consBefore : a -> Zipper a -> Zipper a

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
--> 3
consAfter : a -> Zipper a -> Zipper a

Insert 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
--> 4

Movement

Functions that move focus within Zipper around without losing data.

Bounded Movement

These function will return Nothing when moving out of bounds of Zipper.

nextBy : Int -> Zipper a -> Maybe (Zipper a)

Perform next n times.

prevBy : Int -> Zipper a -> Maybe (Zipper a)

Perform prev n times.

Direction Movement

These 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
--> 1

Move focus to previous value if such value exists.

custom [1] 2 [3]
|> attemptPrev
|> current
|> 1


custom [] 1 []
|> attemptPrev
|> current
--> 1
attemptPrevBy : Int -> Zipper a -> Zipper a

Perform attemptPrev n times.

attemptNextBy : Int -> Zipper a -> Zipper a

Perform attemptNext n times.

Bounds

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
--> 1
end : Zipper a -> Zipper a

Move focus to the very last value

custom [ 1, 2, 3 ] 4 [ 5, 6, 7 ]
|> end
|> current
--> 7

Cycling Movement

These 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
--> 1

Move 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
--> 3
forwardBy : Int -> Zipper a -> Zipper a

Move [forward]{#forward} n times.

backwardBy : Int -> Zipper a -> Zipper a

Move [backward]{#backward} n times.

Tranform

map : (a -> b) -> Zipper a -> Zipper b

Map a function over Zipper

map String.fromInt (custom [1] 2 [3, 4])
|> toList
--> ["1", "2", "3", "4"]
relativeIndexedMap : (Int -> a -> b) -> Zipper a -> Zipper b

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")]
absoluteIndexedMap : (Int -> a -> b) -> Zipper a -> Zipper b

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")]
foldl : (a -> b -> b) -> b -> Zipper a -> b

Recude Zipper from left

foldl (+) 0 <| custom [1,2] 3 [4]
--> 10
foldr : (a -> b -> b) -> b -> Zipper a -> b

Reduce Zipper from right

foldr (+) 0 <| custom [1,2] 3 [4]
--> 10
foldl1 : (a -> a -> a) -> Zipper a -> a

Collapse Zipper a into a value from left

foldl1 (++) <| custom ["hello"] " " ["world"]
--> "world hello"
foldr1 : (a -> a -> a) -> Zipper a -> a

Collapse Zipper a into a value from right

foldr1 (+) (custom [1,2] 3 [4])
--> 10


foldr1 (++) (custom ["hello"] " " ["world"])
--> "hello world"

Conbine

map2 : (a -> b -> c) -> Zipper a -> Zipper b -> Zipper c

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]
andMap : Zipper a -> Zipper (a -> b) -> Zipper b

Map over multiple Zippers.

map (+) (custom [1] 2 [3])
|> andMap (custom [1] 2 [3])
|> toList
--> [2, 4, 6]

Expand

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 []
extend : (Zipper a -> b) -> Zipper a -> Zipper b

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])
duplicateList : Zipper a -> List (Zipper a)

duplicate and covert to List.

This function might be useful in view code.

Convert

toNonEmpty : Zipper a -> NonEmpty a

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])
toList : Zipper a -> List a

Convert Zipper to List.

singleton 1
|> toList
--> [1]


custom [1,2] 3 []
|> toList
--> [1,2,3]