Fumiaki Kinoshita
2017-11-29 04:12:34 UTC
We found this quite useful, but we are not 100% about the name and
documentation.
-- | Tryâin the 'Alternative' senseâto return all the values in a 'Foldable'
-- container.
--
-- @
-- foldAlt â¡ 'listToMaybe' :: [a] -> 'Maybe' a
-- foldAlt â¡ 'maybeToList' :: 'Maybe' a -> [a]
-- foldAlt â¡ 'MaybeT' . 'return' :: ('Monad' m) => 'Maybe' a -> 'MaybeT' m a
-- foldAlt â¡ 'Pipes.ListT' . 'Pipes.each' :: ('Monad' m) => [a] ->
'Pipes.ListT' m a
-- foldAlt â¡ id :: 'Maybe' a -> 'Maybe' a
-- @
foldAlt :: (Foldable t, Alternative f) => t a -> f a
foldAlt = getAlt . foldMap (Alt . pure)
{-# INLINE foldAlt #-}
I propose adding this to either `Data.Foldable` or `Control.Applicative`.
Any thoughts?
documentation.
-- | Tryâin the 'Alternative' senseâto return all the values in a 'Foldable'
-- container.
--
-- @
-- foldAlt â¡ 'listToMaybe' :: [a] -> 'Maybe' a
-- foldAlt â¡ 'maybeToList' :: 'Maybe' a -> [a]
-- foldAlt â¡ 'MaybeT' . 'return' :: ('Monad' m) => 'Maybe' a -> 'MaybeT' m a
-- foldAlt â¡ 'Pipes.ListT' . 'Pipes.each' :: ('Monad' m) => [a] ->
'Pipes.ListT' m a
-- foldAlt â¡ id :: 'Maybe' a -> 'Maybe' a
-- @
foldAlt :: (Foldable t, Alternative f) => t a -> f a
foldAlt = getAlt . foldMap (Alt . pure)
{-# INLINE foldAlt #-}
I propose adding this to either `Data.Foldable` or `Control.Applicative`.
Any thoughts?