Module

Data.Array.Extra.Insert

Package
purescript-arrays-extra
Repository
flip111/purescript-arrays-extra

Functions that insert an element when it can not be found in the array.

#snocWith Source

snocWith :: forall a. (a -> Boolean) -> Array a -> a -> Maybe (Array a)

Append an element to the end of the array when it could not be found by the predicate.

snocWith (_ == 2) [1,3] 2 == Just [1,3,2]

#snocWith' Source

snocWith' :: forall a. Eq a => Array a -> a -> Maybe (Array a)

Append an element to the end of the array when it could not be found.

snocWith' [1,3] 2 == Just [1,3,2]

#consWith Source

consWith :: forall a. (a -> Boolean) -> a -> Array a -> Maybe (Array a)

Find an element by a predicate and when it was not found return an array with the element pushed to the front.

consWith 2 [1,3] == Just [2,1,3]

#consWith' Source

consWith' :: forall a. Eq a => a -> Array a -> Maybe (Array a)

Find an element and when it was not found return an array with the element pushed to the front.

consWith [1,3] 2 == Just [2,1,3]

#insertByWith Source

insertByWith :: forall a. (a -> Boolean) -> (a -> a -> Ordering) -> a -> Array a -> Maybe (Array a)

Insert the element into a sorted array but only if the element can not be found by the predicate.

insertByWith (_ == 2) (\a b -> compare a b) 2 [1,3]  == Just [1,2,3]

#insertByWith' Source

insertByWith' :: forall a. Eq a => (a -> a -> Ordering) -> a -> Array a -> Maybe (Array a)

Insert the element into a sorted array but only if the element didn't exist before.

insertByWith (\a b -> compare a b) 2 [1,3]  == Just [1,2,3]

#insertArray Source

insertArray :: forall a. Int -> Array a -> Array a -> Maybe (Array a)

Insert an array into another array at the given position. Returns Nothing when the index is out of bounds.

unsafeInsertArray 2 [21,22] [1,2,3,4,5] == [1,2,21,22,3,4,5]