sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.03k stars 94 forks source link

Add `asum` #582

Open masaeedu opened 5 years ago

masaeedu commented 5 years ago
// :: (Foldable f, Plus g) => TypeRep g -> f (g a) -> g a
const asum = A => S.reduce(S.alt)(S.zero(A))
masaeedu commented 5 years ago

I should note here that Flutures might be one common Alt that we wish to use this function with, however after some discussion in the Fluture chatroom it was concluded that it doesn't make much sense for the non-concurrent Fluture to implement the full Plus; instead it's only going to implement Alt.

To work with such instances, it might be better for asum to explicitly accept a zero case and weaken the constraint to:

// :: (Foldable f, Alt g) => f (g a) -> g a -> g a