Question

Control.Lens.Fold contains filtered, which I could use to filter a list before applying some monadic action. There doesn't seem to be a corresponding filteredM - but is there a way to get that effect?

To be clear, say I have

xs      :: [ MyType ]
predM   :: MyType -> MyMonad Bool
actionM :: MyType -> MyMonad ()

how can I apply actionM to each element of xs for which predM returns True?

An important constraint is that I want to sequence all invocations of predM before the first invocation of actionM - so I need a way to make two passes over the list. I can't just combine predM and actionM into a single function.

Était-ce utile?

La solution

why not

mapM_ actionM <=< filterM predM $ toListOf YOUR_LENS_HERE YOUR_OBJECT_HERE

or just

mapM_ actionM <=< filterM predM $ xs

if you really only need to work on a list

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top