Module

Data.Zipper.ArrayZipper

Package
purescript-arrays-zipper
Repository
JordanMartinez/purescript-arrays-zipper

#ArrayZipper Source

newtype ArrayZipper a

An immutable Zipper for an Array.

This Zipper works well in read-heavy code but might not work well in write-heavy code

Modifications to the focused element are O(n) due to creating a new immutable array with the change rather than mutating the underlying array.

Navigating to a new focus element is O(1) regardless of how far away from the current focus that element is. This is in contrast to a List-based zipper where modifications are O(1) and navigation is O(n).

[0, 1, 2, 3, 4, 5] <-- underlying array
         ^      ^
         |      -- maxIndex
         -- focusIndex

Instances

#asArrayZipper Source

asArrayZipper :: forall a. a -> ArrayZipper a

Creates an Array Zipper from a single element. This will be stored internally as a 1-element array. To further build upon this array, see push* functions.

#toArrayZipperFirst Source

toArrayZipperFirst :: forall a. Array a -> Maybe (ArrayZipper a)

Returns Nothing if the array is empty. Otherwise, returns an ArrayZipper with the first element as the focus.

#toArrayZipperLast Source

toArrayZipperLast :: forall a. Array a -> Maybe (ArrayZipper a)

Returns Nothing if the array is empty. Otherwise, returns an ArrayZipper with the last element as the focus.

#toArrayZipperAt Source

toArrayZipperAt :: forall a. Int -> Array a -> Maybe (ArrayZipper a)

Returns Nothing if the array is empty. Otherwise, returns an ArrayZipper with the element at the given index as the focus. The given index will be clamped within the array's bounds to ensure it always refers to a valid element in the array. To return Nothing on an invalid index, see toArrayZipperAt'.

#toArrayZipperAt' Source

toArrayZipperAt' :: forall a. Int -> Array a -> Maybe (ArrayZipper a)

Returns Nothing if the array is empty or if the given index is outside the bounds of the array. Otherwise, returns an ArrayZipper with the element at the given index as the focus. To return Just zipper by clamping an invalid index, so that it is within the array, see toArrayZipperAt.

#exposeArray Source

exposeArray :: forall a. ArrayZipper a -> Array a

Exposes the underlying array. Note: any mutations to this array via unsafeThaw will invalidate the constraints guaranteed by ArrayZipper.

#exposeMaxIndex Source

exposeMaxIndex :: forall a. ArrayZipper a -> Int

Exposes the index of the last element

#exposeFocusIndex Source

exposeFocusIndex :: forall a. ArrayZipper a -> Int

Exposes the index of the focused element

#hasPrev Source

hasPrev :: forall a. ArrayZipper a -> Boolean

Returns true if prev will return a Just

#hasNext Source

hasNext :: forall a. ArrayZipper a -> Boolean

Returns true if prev will return a Just

#prev Source

prev :: forall a. ArrayZipper a -> Maybe (ArrayZipper a)

Returns Nothing if the focus element is the first element in the array. Otherwise, returns Just where the new focus element is the previous element.

#next Source

next :: forall a. ArrayZipper a -> Maybe (ArrayZipper a)

Returns Nothing if the focus element is the last element in the array. Otherwise, returns Just where the new focus element is the next element.

#shiftFocusBy Source

shiftFocusBy :: forall a. (Int -> Int) -> ArrayZipper a -> ArrayZipper a

Use a function to focus a different element in the array by using the zipper's current focus index. If the resulting index is outside the bounds of the array, the index will refer to the first or last element, whichever is closer to the output of the function. To prevent clamping and return Nothing if the output of the function is an invalid index, see shiftFocusBy'.

#shiftFocusBy' Source

shiftFocusBy' :: forall a. (Int -> Int) -> ArrayZipper a -> Maybe (ArrayZipper a)

Use a function to focus a different element in the array by using the zipper's current focus index. If the resulting index is outside the bounds of the array, Nothing is returned. If it's a valid index, Just zipper is returned.

#shiftFocusByFind Source

shiftFocusByFind :: forall a. (a -> Boolean) -> ArrayZipper a -> ArrayZipper a

Use a function to find and focus the first matching element in the array. If no element matches, the zipper is returned unchanged.

#shiftFocusByFind' Source

shiftFocusByFind' :: forall a. (a -> Boolean) -> ArrayZipper a -> Maybe (ArrayZipper a)

Use a function to find and the first matching element in the array. If no element matches, Nothing is returned. If an element matches, Just zipper is returned.

#shiftFocusTo Source

shiftFocusTo :: forall a. Eq a => a -> ArrayZipper a -> ArrayZipper a

Find and focus the first equal element in the array. If no element is equal, the zipper is returned unchanged.

#shiftFocusTo' Source

shiftFocusTo' :: forall a. Eq a => a -> ArrayZipper a -> Maybe (ArrayZipper a)

Find and focus the first equal element in the array. If no element is equal, Nothing is returned. If an element is equal, Just zipper is returned.

#shiftFocusFirst Source

shiftFocusFirst :: forall a. ArrayZipper a -> ArrayZipper a

Changes the focus element to the first element in the array.

#shiftFocusLast Source

shiftFocusLast :: forall a. ArrayZipper a -> ArrayZipper a

Changes the focus element to the last element in the array.

#getFocus Source

getFocus :: forall a. ArrayZipper a -> a

Returns the focus element. O(1)

#setFocus Source

setFocus :: forall a. a -> ArrayZipper a -> ArrayZipper a

Sets the focus element. O(n)

#modifyFocus Source

modifyFocus :: forall a. (a -> a) -> ArrayZipper a -> ArrayZipper a

Uses a function to update the focus element. O(n)

#pushPrev Source

pushPrev :: forall a. a -> ArrayZipper a -> ArrayZipper a

Inserts an element in front of / to the left of the focus element. O(n)

#pushNext Source

pushNext :: forall a. a -> ArrayZipper a -> ArrayZipper a

Inserts an element behind / to the right of the focus element. O(n)

#pushPrevRefocus Source

pushPrevRefocus :: forall a. a -> ArrayZipper a -> ArrayZipper a

Inserts an element in front of / to the left of the focus element and sets this new element as the focus element. O(n)

#pushNextRefocus Source

pushNextRefocus :: forall a. a -> ArrayZipper a -> ArrayZipper a

Inserts an element behind / to the right of the focus element and sets this new element as the focus element. O(n)