Monadic version of functions for containers (adjustM, updateWithKeyM, etc)

Thomas DuBuisson thomas.dubuisson at gmail.com
Mon Sep 27 18:47:19 EDT 2010


On Mon, Sep 27, 2010 at 3:16 PM, Felipe Lessa <felipe.lessa at gmail.com> wrote:
>>    adjustM f k m == liftM (\x -> insert k x m) (f $ lookup k m )
>
> This suggests that you don't need the Monad constraint, only Functor.

That's just because I hid the case where `lookup k m` fails.  I'm sure
looking at other desirable *M functions would reveal similar issues
with using Functor alone.

    adjustM f k m ==
        case lookup k m of
              Just x -> liftM (\new -> insert k new m) (f x)
              Nothing -> return m

translating this to Functor:

    adjustF f k m ==
        case lookup k m of
            Just x -> fmap (\new -> insert k new m) (f x)
            Nothing -> ???  -- perhaps "pure m" thus adding another
constraint, but how would that be better than Monad for most people?

Cheers,
Thomas


More information about the Libraries mailing list