How do you extract a value out of the Shell monad?
I would like to sequence a list of commands à la bash's &&
, but I would also like to extract the final ExitCode
value.
Say I have the following code:
import Turtle
type Commands = [Shell ExitCode]
run :: (MonadIO io) => Commands -> io ExitCode
run cs = whatIsThisFunction $ Prelude.foldl (.&&.) (return ExitSuccess) cs
whatIsThisFunction :: (MonadIO io) => Shell a -> io a
whatIsThisFunction = undefined
I tried to see if I could implement this with Control.Foldl, but did not find a solution.
Any ideas?
More generally, why doesn't Turtle provide a function with such signature:
sh' :: MonadIO io => Shell a -> io a
Turtle.Shell
provides you with a fold :: MonadIO io => Shell a -> Fold a b -> io b
and Control.Foldl
gives you a bunch of Fold
s amongst which: last :: Fold a (Maybe a)
. You can combine the two to extract the last ExitCode
your command returns like so:
import Control.Monad.IO.Class
import Turtle.Shell as TS
import Control.Foldl as CF
sh' :: MonadIO io => Shell a -> io (Maybe a)
sh' c = TS.fold c CF.last
sh' :: MonadIO io => Shell a -> io a
is not possible because Shell a
may be constructed from [a]
(evidenced by select :: [a] -> Shell a
) which can be empty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With