How to generalize executing a series of commands, based on a list?

I am able to use System.Cmd (system) to invoke a shell command and interpret the results. Please see the code below that works okay for one such command. (I invoke a program, passing two args.) I am wondering how to generalize this to do likewise for a series of commands, where the varying args (filenames, in this case) are in a list ('inOutLeafs'). I will also want to accumulate some results; probably just a failure count at this time. Any advice or pointers to examples would be much appreciated. Thanks in advance, -- Peter
run :: ... -> IO (Int) -- will return a fail count run -- some args to this function here... = do -- ... set up: inputLeafs, outputLeafs, etc.
-- zip two lists of filenames: let inOutLeafs = zip inputLeafs outputLeafs
-- the first pair for the first command: let (inFile1,outFile1) = head inOutLeafs
-- build 1st command using 1st pair of filenames: let cmd1 = ...
exitCode <- system cmd1 case (exitCode) of ExitSuccess -> do putStrLn $ "-- OK." return 0 ExitFailure failCnt -> do putStrLn $ "-- Failed: " ++ show failCnt return 1

On 2010-11-17 21:03, Peter Schmitz wrote:
I am wondering how to generalize this to do likewise for a series of commands, where the varying args (filenames, in this case) are in a list ('inOutLeafs').
The 'sequence' function is handy for combining a series of actions, such as [system cmd1, system cmd2, ...].
I will also want to accumulate some results; probably just a failure count at this time.
'sequence' hangs on to the results. That may be what you need. For control over accumulating results the good stuff is in Data.Foldable.
Any advice or pointers to examples would be much appreciated.
Thanks in advance, -- Peter
run :: ... -> IO (Int) -- will return a fail count run -- some args to this function here... = do -- ... set up: inputLeafs, outputLeafs, etc.
-- zip two lists of filenames: let inOutLeafs = zip inputLeafs outputLeafs
-- the first pair for the first command: let (inFile1,outFile1) = head inOutLeafs
-- build 1st command using 1st pair of filenames: let cmd1 = ...
exitCode <- system cmd1 case (exitCode) of ExitSuccess -> do putStrLn $ "-- OK." return 0 ExitFailure failCnt -> do putStrLn $ "-- Failed: " ++ show failCnt return 1
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thank you very much for the help. -- Peter On Thu, Nov 18, 2010 at 4:48 AM, Scott Turner <1haskell@pkturner.org> wrote:
On 2010-11-17 21:03, Peter Schmitz wrote:
I am wondering how to generalize this to do likewise for a series of commands, where the varying args (filenames, in this case) are in a list ('inOutLeafs').
The 'sequence' function is handy for combining a series of actions, such as [system cmd1, system cmd2, ...].
I will also want to accumulate some results; probably just a failure count at this time.
'sequence' hangs on to the results. That may be what you need. For control over accumulating results the good stuff is in Data.Foldable.
Any advice or pointers to examples would be much appreciated.
Thanks in advance, -- Peter
run :: ... -> IO (Int) -- will return a fail count run -- some args to this function here... = do -- ... set up: inputLeafs, outputLeafs, etc.
-- zip two lists of filenames: let inOutLeafs = zip inputLeafs outputLeafs
-- the first pair for the first command: let (inFile1,outFile1) = head inOutLeafs
-- build 1st command using 1st pair of filenames: let cmd1 = ...
exitCode <- system cmd1 case (exitCode) of ExitSuccess -> do putStrLn $ "-- OK." return 0 ExitFailure failCnt -> do putStrLn $ "-- Failed: " ++ show failCnt return 1
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

1. Write one routine, which does all the work for just one command.
2. use sequence or mapM, mapM_ from Control.Monad (depending on your
needs),
to apply your function to a list of commands
accumulating results you may want to process the output of "sequence"
or use the WriterT Monad Transformer.
If you want to stop processing the rest of the list on error, either
write a recursive function yourself or use foldM or use ErrorT Monad
Transformer.
On Nov 18, 3:03 am, Peter Schmitz
I am able to use System.Cmd (system) to invoke a shell command and interpret the results.
Please see the code below that works okay for one such command. (I invoke a program, passing two args.)
I am wondering how to generalize this to do likewise for a series of commands, where the varying args (filenames, in this case) are in a list ('inOutLeafs').
I will also want to accumulate some results; probably just a failure count at this time.
Any advice or pointers to examples would be much appreciated.
Thanks in advance, -- Peter
run :: ... -> IO (Int) -- will return a fail count run -- some args to this function here... = do -- ... set up: inputLeafs, outputLeafs, etc.
-- zip two lists of filenames: let inOutLeafs = zip inputLeafs outputLeafs
-- the first pair for the first command: let (inFile1,outFile1) = head inOutLeafs
-- build 1st command using 1st pair of filenames: let cmd1 = ...
exitCode <- system cmd1 case (exitCode) of ExitSuccess -> do putStrLn $ "-- OK." return 0 ExitFailure failCnt -> do putStrLn $ "-- Failed: " ++ show failCnt return 1
_______________________________________________ Haskell-Cafe mailing list Haskell-C...@haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe

Thank you very much for the help. Good tips for improving my code design.
I'm new to sequence, mapM, and mapM_; I've seen mapM a lot while reading
code and wanted to learn it; now I have a reason! Thanks.
-- Peter
On Thu, Nov 18, 2010 at 5:40 AM, steffen
1. Write one routine, which does all the work for just one command. 2. use sequence or mapM, mapM_ from Control.Monad (depending on your needs), to apply your function to a list of commands
accumulating results you may want to process the output of "sequence" or use the WriterT Monad Transformer.
If you want to stop processing the rest of the list on error, either write a recursive function yourself or use foldM or use ErrorT Monad Transformer.
On Nov 18, 3:03 am, Peter Schmitz
wrote: I am able to use System.Cmd (system) to invoke a shell command and interpret the results.
Please see the code below that works okay for one such command. (I invoke a program, passing two args.)
I am wondering how to generalize this to do likewise for a series of commands, where the varying args (filenames, in this case) are in a list ('inOutLeafs').
I will also want to accumulate some results; probably just a failure count at this time.
Any advice or pointers to examples would be much appreciated.
Thanks in advance, -- Peter
run :: ... -> IO (Int) -- will return a fail count run -- some args to this function here... = do -- ... set up: inputLeafs, outputLeafs, etc.
-- zip two lists of filenames: let inOutLeafs = zip inputLeafs outputLeafs
-- the first pair for the first command: let (inFile1,outFile1) = head inOutLeafs
-- build 1st command using 1st pair of filenames: let cmd1 = ...
exitCode <- system cmd1 case (exitCode) of ExitSuccess -> do putStrLn $ "-- OK." return 0 ExitFailure failCnt -> do putStrLn $ "-- Failed: " ++ show failCnt return 1
_______________________________________________ Haskell-Cafe mailing list Haskell-C...@haskell.orghttp:// www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Thu, 18 Nov 2010 21:20:10 +0100, Peter Schmitz
Thank you very much for the help. Good tips for improving my code design. I'm new to sequence, mapM, and mapM_; I've seen mapM a lot while reading code and wanted to learn it; now I have a reason! Thanks. -- Peter
You can find explanations, with usage examples, of these functions at http://members.chello.nl/hjgtuyl/tourdemonad.html Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --

Thanks very much for the url; helpful site.
I got mapM to do what I wanted (first time I've used it); cool function; now
I understand it.
At your note for mapM I noticed (mapM print);
I've been using (show [list]) so much, (mapM print) will be nice for
formatting while debugging, etc.
Thanks again!
-- Peter
On Thu, Nov 18, 2010 at 3:44 PM, Henk-Jan van Tuyl
On Thu, 18 Nov 2010 21:20:10 +0100, Peter Schmitz
wrote: Thank you very much for the help. Good tips for improving my code design.
I'm new to sequence, mapM, and mapM_; I've seen mapM a lot while reading code and wanted to learn it; now I have a reason! Thanks. -- Peter
You can find explanations, with usage examples, of these functions at http://members.chello.nl/hjgtuyl/tourdemonad.html
Regards, Henk-Jan van Tuyl
-- http://Van.Tuyl.eu/ http://van.tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --
participants (4)
-
Henk-Jan van Tuyl
-
Peter Schmitz
-
Scott Turner
-
steffen