ListZipper-1.2.0.2: Simple zipper for lists

Data.List.Zipper

Synopsis

Documentation

data Zipper a Source

Constructors

Zip ![a] ![a] 

Instances

empty :: Zipper aSource

empty is an empty zipper

fromList :: [a] -> Zipper aSource

fromList xs returns a zipper containing the elements of xs, focused on the first element.

fromListEnd :: [a] -> Zipper aSource

fromListEnd xs returns a zipper containing the elements of xs, focused just off the right end of the list.

toList :: Zipper a -> [a]Source

beginp :: Zipper a -> BoolSource

beginp z returns True if the zipper is at the start.

endp :: Zipper a -> BoolSource

endp z returns True if the zipper is at the end. It is not safe to call cursor on z if endp z returns True.

emptyp :: Zipper a -> BoolSource

emptyp z returns True if the zipper is completely empty. forall z. emptyp z == beginp z && endp z

cursor :: Zipper a -> aSource

cursor z returns the targeted element in z.

This function is not total, but the invariant is that endp z == False means that you can safely call cursor z.

safeCursor :: Zipper a -> Maybe aSource

safeCursor is like cursor but total.

left :: Zipper a -> Zipper aSource

left z returns the zipper with the focus shifted left one element.

right :: Zipper a -> Zipper aSource

right z returns the zipper with the focus shifted right one element; this can move the cursor off the end.

insert :: a -> Zipper a -> Zipper aSource

insert x z adds x at the cursor.

delete :: Zipper a -> Zipper aSource

delete z removes the element at the cursor (if any). Safe to call on an empty zipper. forall x z. delete (insert x z) == z

push :: a -> Zipper a -> Zipper aSource

push x z inserts x into the zipper, and advances the cursor past it.

pop :: Zipper a -> Zipper aSource

pop z removes the element before the cursor (if any). Safe to call on an empty zipper. forall x z. pop (push x z) == z

replace :: a -> Zipper a -> Zipper aSource

replace a z changes the current element in the zipper to the passed in value. If there is no current element, the zipper is unchanged. If you want to add the element in that case instead, use insert a (delete z).

reversez :: Zipper a -> Zipper aSource

reversez z returns the zipper with the elements in the reverse order. O(1). The cursor is moved to the previous element, so if the cursor was at the start, it's now off the right end, and if it was off the right end, it's now at the start of the reversed list.

foldrz :: (Zipper a -> b -> b) -> b -> Zipper a -> bSource

foldrz f x zip calls f with the zipper focused on each element in order, starting with the current. You are guaranteed that f can safely call cursor on its argument; the zipper won't be at the end.

foldlz :: (b -> Zipper a -> b) -> b -> Zipper a -> bSource

foldlz f x zip calls f with the zipper focused on each element in order, starting with the current. You are guaranteed that f can safely call cursor on its argument; the zipper won't be at the end.

foldlz' :: (b -> Zipper a -> b) -> b -> Zipper a -> bSource

foldlz' is foldlz with a strict accumulator

extractz :: Zipper a -> aSource

extractz, extendz, and duplicatez can be used to implement Copointed and Comonad from category-extras. I didn't add the instances here so as not to introduce a dependency on that package.

extendz :: (Zipper a -> b) -> Zipper a -> Zipper bSource