Data.Array.Extra
- Package
- purescript-arrays-extra
- Repository
- flip111/purescript-arrays-extra
Some specialized functions can be found here.
#sortWithBy Source
sortWithBy :: forall a b. (a -> b) -> (b -> b -> Ordering) -> Array a -> Array a
Make a projection of an array then sort the original array by the projection
sortWithBy (\x -> if x == "dog" then 2 else 1) compare ["apple", "dog", "kiwi"] = ["apple", "kiwi", "dog"]
#differenceBy Source
differenceBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array a
Like difference
but takes a comparison function.
differenceBy (\a b -> toLower a == toLower b) ["apple", "dog"] ["KIWI", "DOG"] == ["KIWI"]
#partitionMaybe Source
partitionMaybe :: forall a b. (a -> Maybe b) -> Array a -> { no :: Array a, yes :: Array b }
Try to make a projection from an array. Return an array with the projection and the original array with the elements removed.
partitionMaybe (\x -> if x == "dog" then Just "cat" else Nothing) ["apple", "dog", "kiwi"] == { no: ["apple", "kiwi"], yes: ["cat"] }
#firstMaybe Source
firstMaybe :: forall a b. (a -> Maybe b) -> Array a -> Maybe b
Find an element which could be projected into another value.
firstMaybe (\x -> if x == 2 then Just "Found two" else Nothing) [1,2,3] == Just "Found Two"
#unionByWhen Source
unionByWhen :: forall a. (a -> a -> Boolean) -> (a -> Boolean) -> Array a -> Array a -> Array a
Like unionBy between array A and array B. With elements left out from B being included when they match the predicate.
Useful for updating matching elements and at the same time inserting new ones that match the insert criteria
unionByWhen
(\a b -> a.id == b.id)
(\{t} -> not $ null t)
[{id: 1, t: "old"}, {id: 2, t: "old"}]
[{id: 2, t: "new"}, {id: 3, t: ""}, {id: 4, t: "new"}]
= [{id: 1, t: "old"}, {id: 2, t: "new"}, {id: 4, t: "new"}]
Truth table | in A | not in A | in B | update | insert-when | not in B | keep | n/a |
#mapEither Source
mapEither :: forall a b c. (a -> Either c b) -> Array a -> Either (Array c) (Array b)
Map with a function that yields Either
. Only succeeding when all elements where mapped to Right
.
Hint: if you don't care about collecting all the Left's (error conditions) and you are looking for a function like
forall a b c. (a -> Either c b) -> Array a -> Either c (Array b)
then use traverse
from Data.Traversable
.
#occurrencesMap Source
occurrencesMap :: forall a. Ord a => Array a -> Map a Int
Count the amount of times a value occurs in an array.
Requires an Ord instance for Map. This function should be faster than occurrences
occurrencesMap ["A", "B", "B"] == Map.fromList [Tuple "A" 1, Tuple "B" 2]