[Haskell-cafe] MonadPlus and Alternative folds
j at dannynavarro.net
Sat Sep 19 07:22:08 UTC 2015
Not sure about performance, but you could wrap the `Alternative` with
an `Alt` and use the `Foldable` instance.
There is also `Alternative` folding in the `reducers` package.
On Sat, Sep 19, 2015 at 7:25 AM, David Feuer <david.feuer at gmail.com> wrote:
> It kind of seems like there should be fold-like things to match some and
> 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.
> David Feuer
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
Danny Navarro | http://dannynavarro.net
More information about the Haskell-Cafe