I have a set of five functions, that could be called one of five ways. I'm expressing that with patern-matching like so,
type Configure = ReaderT Config IO ()
data Step = PreVal
| PreProc
| Proc
| PostProc
| PostVal
foo :: Step -> Configure
foo PreVal = do some stuff
foo PreProc = do some stuff
and so on bar
and baz
are set up similarly
I know how to use sequence
to call a list of actions. Given a [Step]
, how could I go about calling [foo,bar,baz]
. in sequence, while also calling each possible step.
so it should do this
foo PreVal
foo PreProc
... and so on
bar Preval
bar PreProc
.. and so on
baz
...
mapM_ (\ f -> mapM_ f [PreVal, PreProc, Proc, PostProc, PostVal]) [foo, bar, baz]
I would like to add something to the previous answers.
As long as the order of your value constructors is the same as the order of execution of the steps, you could specify deriving (Enum)
. This would allow you to write the list of all Step
s as [PreVal..PostVal]
and shorten the code.
Furthermore, consider a case in which you add a step before PreVal
or after PostVal
. To be sure that your calls consider the newly introduced step, you would better define an instance for Bounded
too, and then use minBound
and maxBound
in the code.
doThemAll steps = sequence_ $ do
f <- [foo, bar, baz]
step <- steps
return (f step)
sequence_ $ [foo, bar, baz] `ap` steps
(or with <*>
instead of ap
- hammar 2012-04-04 22:20