Haskell:Turtle:从Shell中获取返回值

dam*_*sse 4 haskell haskell-turtle

如何从Shell monad中提取一个值?

我想排序的命令列表点菜 bash的&&,但我也想提取最终ExitCode值.

说我有以下代码:

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
Run Code Online (Sandbox Code Playgroud)

我试图看看我是否可以用Control.Foldl实现它,但没有找到解决方案.

有任何想法吗?

更一般地说,为什么Turtle没有提供具有这种签名的功能:

sh' :: MonadIO io => Shell a -> io a 
Run Code Online (Sandbox Code Playgroud)

gal*_*ais 5

Turtle.Shell为你提供一个fold :: MonadIO io => Shell a -> Fold a b -> io bControl.Foldl给你一堆Folds:其中:last :: Fold a (Maybe a).您可以将两者结合起来提取最后一个ExitCode命令返回,如下所示:

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
Run Code Online (Sandbox Code Playgroud)


eri*_*sco 3

sh' :: MonadIO io => Shell a -> io a是不可能的,因为可以由可以为空的(由 证明)Shell a构造。[a]select :: [a] -> Shell a