[Haskell-cafe] MonadPlus and Alternative folds
David Feuer
david.feuer at gmail.com
Sat Sep 19 05:25:00 UTC 2015
It kind of seems like there should be fold-like things to match some and
many.
foldrAlt :: Alternative f => (a -> b -> b) -> b -> f a -> f b
foldrAlt c n m = foldr1Alt c n m <|> pure n
foldr1Alt :: Alternative f => (a -> b -> b) -> b -> f a -> f b
foldr1Alt c n m = c <$> m <*> foldrAlt c n m
foldlMP' :: MonadPlus f => (b -> a -> b) -> b -> f a -> f b
foldlMP' f b m = foldl1MP' f b m <|> pure b
foldl1MP' :: MonadPlus f => (b -> a -> b) -> b -> f a -> f b
foldl1MP' f b m = m >>= \r -> let !b' = f b r in foldlMP' f b' m
--These look a bit iffy
foldlAlt :: Alternative f => (b -> a -> b) -> b -> f a -> f b
foldlAlt f b m = ($ b) <$> foldrAlt (\x r acc -> r (f acc x)) id m
foldl1Alt :: Alternative f => (b -> a -> b) -> b -> f a -> f b
foldl1Alt f b m = ($ b) <$> foldr1Alt (\x r acc -> r (f acc x)) id m
Do these or similar exist somewhere? If not, should they exist somewhere?
I'm particularly interested in whether either of these will be fast, or
will admit a fast implementation, for attoparsec parsers.
Thanks,
David Feuer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150919/03684a8f/attachment.html>
More information about the Haskell-Cafe
mailing list