Module

Data.Foldable.Extra

Package
purescript-foldable-traversable-extra
Repository
flip111/purescript-foldable-traversable-extra

#partitionMaybe Source

partitionMaybe :: forall a b f. Foldable f => (a -> Maybe b) -> f 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"] }

#mapMaybeAny Source

mapMaybeAny :: forall f a. Foldable f => (a -> Maybe a) -> f a -> Maybe (Array a)

Map an array conditionally, only return the array when at least one element was mapped. Elements that are not mapped will keep the old value.

mapMaybeAny (\_ -> Nothing) [1,2,3] == Nothing
mapMaybeAny (\x -> if x == 2 then Just 99 else Nothing) [1,2,3] == Just [1,99,3]

#mapEither Source

mapEither :: forall a b c f. Foldable f => (a -> Either c b) -> f 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.

#occurrences Source

occurrences :: forall a f. Eq a => Foldable f => f a -> Array (Tuple a Int)

Count the amount of times a value occurs in an array. Mostly useful for when you can not define an Ord instance

occurrences ["A", "B", "B"] == [Tuple "A" 1, Tuple "B" 2]

#occurrencesMap Source

occurrencesMap :: forall a f. Foldable f => Ord a => f 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]

#sameElements Source

sameElements :: forall a f. Foldable f => Eq a => f a -> f a -> Boolean

Checks if two arrays have exactly the same elements. The order of elements does not matter.

sameElements ["A", "B", "B"] ["B", "A", "B"] == true
sameElements ["A", "B", "B"] ["A", "B"] == false

#groupMaybe Source

groupMaybe :: forall f a b. Foldable f => Eq b => (a -> Maybe b) -> f a -> Array (Tuple b (NonEmptyArray a))

Similar to group, adds the ability to group by a projection. The projection is returned as the first argument of the Tuple.

groupMaybe (\x -> Just $ if even x then "even" else "odd") [1,2,3] == [(Tuple "odd" [1,3]), (Tuple "even" [2])]

#groupMaybeMap Source

groupMaybeMap :: forall a b c f. Foldable f => Eq b => (a -> Maybe b) -> (a -> c) -> f a -> Array (Tuple b (NonEmptyArray c))

Similar to groupMaybe, adds the ability to map over the thing being grouped. Useful for removing data that was only there to do the grouping.

groupMaybeMap (\x -> Just $ if even x then "even" else "odd") (*3) [1,2,3] == [(Tuple "odd" [3,9]), (Tuple "even" [6])]
groupMaybeMap f identity xs = groupMaybe f xs

#allPredicate Source

allPredicate :: forall f a. Foldable f => f (a -> Boolean) -> (a -> Boolean)

Combines multiple predicates into one. All have to match. This function is an alias for Foldable.and, since HeytingAlgebras lift over functions.

let preds = allPredicate [(_ > 5), (_ > 10)]
all preds [10,20] == true
all preds [5,10,20] == false
any preds [1,5,10] == true
any preds [1,5] == false

#anyPredicate Source

anyPredicate :: forall f a. Foldable f => f (a -> Boolean) -> (a -> Boolean)

Combines multiple predicates into one. Only one has to match. This function is an alias for Foldable.or, since HeytingAlgebras lift over functions.

let preds = anyPredicate [(_ > 5), (_ > 10)]
all preds [5,10] == true
all preds [1,5,10] == false
any preds [1,5] == true
any preds [1] == false