Module

Data.Array.Extra.First

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

Functions that find the first matching element based on a predicate and then do something with the array.

#partitionFirst Source

partitionFirst :: forall a. (a -> Boolean) -> Array a -> Maybe { no :: Array a, yes :: a }

Find an element by a predicate and return it together with the array without the element.

partitionFirst (== 2) [1,2,3] == Just {yes: 2, no: [1,3]}

#deleteFirstWith Source

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

Find an element by a predicate and return an array without that element when it was found.

deleteWith (== 2) [1,2,3] == Just [1,3]

#updateFirstWith Source

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

Find an element by a predicate and return an array with the updated element when it was found

updateWith (== 2) 4 [1,2,3] == Just [1,4,3]

#updateFirstArrayWith Source

updateFirstArrayWith :: forall a. Partial => (a -> Boolean) -> Array a -> Array a -> Maybe (Array a)

Find an element by a predicate and return an array with the element replaced by an array.

updateFirstArrayWith (== 2) [21,22] [1,2,3,4,5] == Just [1,2,21,22,3,4,5]

#modifyFirstWith Source

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

Find an element by a predicate and return an array with the updated element when it was found

modifyWith (== 2) (* 3) [1,2,3] == Just [1,6,3]

#modifyOrSnoc Source

modifyOrSnoc :: forall a. (a -> Boolean) -> (a -> a) -> Array a -> a -> Array a

Modify an element when it was found by the predicate or append a new element to the end of the array.

modifyOrSnoc (== 2) (* 3) [1,2,3] 11 == Just [1,6,3]
modifyOrSnoc (== 4) (* 3) [1,2,3] 11 == Just [1,2,3,11]

#modifyOrCons Source

modifyOrCons :: forall a. (a -> Boolean) -> (a -> a) -> a -> Array a -> Array a

Modify an element when it was found by the predicate or push a new element to the front of the array.

modifyOrCons (== 2) (* 3) 11 [1,2,3] == Just [1,6,3]
modifyOrCons (== 4) (* 3) 11 [1,2,3] == Just [11,1,2,3]

#updateOrSnoc Source

updateOrSnoc :: forall a. (a -> Boolean) -> Array a -> a -> Array a

Update an element when it was found by the predicate or append a new element to the end of the array.

updateOrSnoc (== 2) [1,2,3] 11 == Just [1,11,3]
updateOrSnoc (== 4) [1,2,3] 11 == Just [1,2,3,11]

#updateOrCons Source

updateOrCons :: forall a. (a -> Boolean) -> a -> Array a -> Array a

Update an element when it was found by the predicate or push a new element to the front of the array.

updateOrCons (== 2) 11 [1,2,3] == Just [1,11,3]
updateOrCons (== 4) 11 [1,2,3] == Just [11,1,2,3]