Geo*_*rge 2 haskell haskell-turtle
考虑(inshell "echo A line of text." empty)有类型Shell Line。
问题:如何将这种类型的值转换为Text?
你不能 请参阅* 的定义Shell。
newtype Shell a = Shell { _foldIO :: forall r . FoldM IO a r -> IO r }
Run Code Online (Sandbox Code Playgroud)
您只能摆脱IO麻烦。但是,您可能会得到一个IO Text,并且由于您正在执行Shell操作,所以我怀疑您IO周围有monad上下文。
FoldM 定义为*
data FoldM m a b =
-- FoldM step initial extract
forall x . FoldM (x -> a -> m x) (m x) (x -> m b)
Run Code Online (Sandbox Code Playgroud)
因此FoldM IO Line Text,我怀疑,如果我们可以构造a,那么我们就能满足需要。这是一个猜测(未经类型检查,这变得越来越复杂,所以我可能会犯错误)。
shellToText :: Shell Text -> IO Text
shellToText shell = foldIO shell foldm
where
foldm :: FoldM IO Line Text
foldm = FoldM (\accum line -> return (accum <> lineToText line)) -- Text -> Line -> IO Text
(return Text.empty) -- IO Text
return -- Text -> IO Text
Run Code Online (Sandbox Code Playgroud)
通过使用中的组合器,Control.Foldl可以大大简化此过程,但我将其保留为练习。
(*)如果您不熟悉,第一个forall表示等级2类型,第二个表示存在类型。
小智 5
strict救援功能:
strict :: MonadIO io => Shell Line -> io Text
所以你可以
strict $ inshell "echo a line of text." empty
并在 IO Text monad 中获取 shell 命令的输出。
| 归档时间: |
|
| 查看次数: |
254 次 |
| 最近记录: |