Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foldlWithKey in monad

I'm looking for a function that like foldlWithKey, but encapsulated in a monad.

I would expect it to have type

Monad m => (a -> k -> b -> m a) -> a -> Map k b -> m a

but Hoogle isn't giving me anything with that type.

like image 626
Mathias Vorreiter Pedersen Avatar asked Apr 04 '15 16:04

Mathias Vorreiter Pedersen


1 Answers

foldlWithKey is already very close to what you want. If you specialize a to m a you will have something that operates on values encapsulated in a monad.

foldlWithKey :: (  a -> k -> b ->   a) ->   a -> Map k b ->   a
foldlWithKey :: (m a -> k -> b -> m a) -> m a -> Map k b -> m a
             {-  ^- you don't want these -^   -}

We can get rid of the two m as you don't want with >>= and return.

foldlWithKeyM :: Monad m => (a -> k -> b -> m a) -> a -> Map k b -> m a
foldlWithKeyM f acc = foldlWithKey f' (return acc) 
    where
        f' ma k b = ma >>= \a -> f a k b
like image 93
Cirdec Avatar answered Sep 30 '22 16:09

Cirdec