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.

#pick Source

pick :: 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.

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

#delete Source

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

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

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

#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]

#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]

#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]

#splitOn Source

splitOn :: forall a. (a -> Boolean) -> Array a -> Maybe { after :: Array a, before :: Array a, found :: a }

Finds a single element and returns it together with elements before and after.

partitionSides (_ == 3) [1,2,3,4,5] == Just {before: [1,2], found: 3, after: [4,5]}

#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]

#findMaybe Source

findMaybe :: forall a b. (a -> Maybe b) -> Array a -> Maybe b

Find an element which could be projected into another value.

findMaybe (\x -> if x == 2 then Just "Found two" else Nothing) [1,2,3] == Just "Found Two"
findMaybe (\x -> if x == 2 then Just 2 else Nothing) [1,2,3] == find (_ == 2) [1,2,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"]