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.
#deleteWith Source
deleteWith :: 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) [2,1,3,2] == Just [1,3,2]
#updateWith Source
updateWith :: forall a. (a -> Boolean) -> a -> Array a -> Maybe (Array a)
Find an element by a predicate, replace it with another element and return the updated array when it was found
updateWith (_ == 2) 4 [1,2,3] == Just [1,4,3]
#modifyWith Source
modifyWith :: forall a. (a -> Boolean) -> (a -> a) -> Array a -> Maybe (Array a)
Find an element by a predicate, apply a function to it and return the updated array 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]
#insertBefore Source
insertBefore :: forall a. (a -> Boolean) -> a -> Array a -> Maybe (Array a)
Find an element and place an element before it. Could also be thought of as placing the element in the place of the found element and moving al later elements.
insertBefore (_ == 2) 10 [1,2,3] == Just [1,10,2,3]
#insertAfter Source
insertAfter :: forall a. (a -> Boolean) -> a -> Array a -> Maybe (Array a)
Find an element and place an element after it.
insertAfter (_ == 2) 10 [1,2,3] == Just [1,2,10,3]
#difference Source
difference :: forall a. Eq a => Array a -> Array a -> Array a
Re-export of difference
from Data.Array
difference [2, 1] [2, 3] = [1]
#differenceBy Source
differenceBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array a
Like difference
but takes a comparison function.
For each element in the second list, one element in the first list will be removed if the predicate matches.
differenceBy (\a b -> toLower a == toLower b) ["apple", "dog"] ["KIWI", "DOG"] == ["apple"]