- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以在 Turtle 库(Haskell)中拆分 Shell,并对任一拆分的 Shell 执行不同的操作,以便原始 Shell 只运行一次?
/---- shell2
---Shell1 --/
\
\-----shell3
例如,如何做
do
let lstmp = lstree "/tmp"
view lstmp
view $ do
path <- lstmp
x <- liftIO $ testdir path
return x
这样 lstree "/tmp"只会运行一次。
具体来说,我想使用输出将 Shell 2 和 Shell 3 发送到不同的文件。
最佳答案
您将无法拆分 Shell
进入两个同时运行的独立外壳,除非有一些我不知道的魔法。但是文件写入是 shell 或其他一些连续事物的内容的折叠。它内置于 turtle
您始终可以组合多个折叠并使用 Control.Foldl
让它们同时运行 Material - 这里
foldIO :: Shell a -> FoldM IO a r -> IO r -- specializing
shell secret 是 FoldM IO a r -> IO r
无论如何,这基本上是 runShell
。为此,我们需要获得正确的 Shell
和右边组合FoldM IO
。整体思路Fold a b
和FoldM m a b
来自 foldl
的类型包装同时折叠。
我认为获得正确 shell 的最简单方法就是制作 lstree
折叠返回 FilePath
一起,结果为 testdir
。你基本上是这样写的:
withDirInfo :: FilePath -> Shell (Bool, FilePath)
withDirInfo tmp = do
let lstmp = lstree tmp
path <- lstmp
bool <- liftIO $ testdir path
return (bool, path)
现在我们可以得到 Shell (Bool, FilePath)
来自/tmp
这包含了我们的两个折叠所需的所有信息,因此我们的组合折叠也需要。
接下来我们可能会编写一个辅助折叠来打印 Text
FilePath
的组成部分到给定的句柄:
sinkFilePaths :: Handle -> FoldM IO FilePath ()
sinkFilePaths handle = L.sink (T.hPutStrLn handle . format fp)
然后我们可以使用这个Handle -> FoldM IO FilePath ()
定义两个FoldM IO (Bool, FilePath) ()
。每个都会向不同的句柄写入不同的内容,我们可以使用 <*
将它们合并到一个同时折叠中。 。这是一个独立的FoldM IO ...
并且可以应用例如到类型 [(Bool, FilePath)]
的纯列表使用L.fold
它会将列表中的不同内容写入不同的句柄。但在我们的例子中,我们将把它应用到 Shell (Bool, FilePath)
我们定义了。
其中唯一微妙的部分是 L.handlesM
的使用在这两种情况下仅打印第二个元素,并且仅打印在另一种情况下过滤为目录的元素。这使用 _2
镜头和filtered
来自镜头库。这可能可以简化,但看看你的想法:
{-#LANGUAGE OverloadedStrings #-}
import Turtle
import qualified Control.Foldl as L
import qualified System.IO as IO
import Control.Lens (_2,filtered)
import qualified Data.Text.IO as T
main = IO.withFile "tmpfiles.txt" IO.WriteMode $ \h ->
IO.withFile "tmpdirs.txt" IO.WriteMode $ \h' -> do
foldIO (withDirInfo "/tmp") (sinkFilesDirs h h')
withDirInfo :: Turtle.FilePath -> Shell (Bool, Turtle.FilePath)
withDirInfo tmp = do
let lstmp = lstree tmp
path <- lstmp
bool <- liftIO $ testdir path
return (bool, path)
sinkFilePaths :: Handle -> FoldM IO Turtle.FilePath ()
sinkFilePaths handle = L.sink (T.hPutStrLn handle . format fp)
sinkFilesDirs :: Handle -> Handle -> FoldM IO (Bool, Turtle.FilePath) ()
sinkFilesDirs h h' = allfiles <* alldirs where
allfiles :: L.FoldM IO (Bool, Turtle.FilePath) ()
allfiles = L.handlesM _2 (sinkFilePaths h)
-- handle the second element of pairs with sinkFilePaths
alldirs :: FoldM IO (Bool, Turtle.FilePath) ()
alldirs = L.handlesM (filtered (\(bool,file) -> bool) . _2) (sinkFilePaths h')
-- handle the second element of pairs where the first element
-- is true using sinkFilePaths
关于Haskell Turtle - 劈开一个壳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37324186/
我实际上正在致力于在 shell 中实现多管道,我必须说我正在解决我在项目中遇到的一些问题。 一个特殊的细节使得这个多管道实现相当困难,它必须在文件描述符的非常低的限制下工作。 例如,如果您在 bas
我正在编写自己的简单 shell,目前我正在考虑从用户那里获取输入(命令)。 我编写了以下原型(prototype): while(1) { printf("gsh> ");
运行 spark-shell与斯卡拉。 Scala 有 -Dscala.color彩色 REPL 的参数。我想要同样的 Spark 。试过 spark-shell -Dscala.color但这不是获
我正在进行一个Electron项目,该项目的主要重点是向用户展示RTMP格式的视频流。 我一直在遵循一些指南; atom/electron’s official guide; “Using Peppe
当使用“ipython”或“code.interact(local=locals())”时,我希望有一种方法可以保存整个程序地址空间到 pickle 文件中,以及类似的加载此类文件然后在该上下文中开始
我有一个要使用 Spark shell 执行的 Scala 程序,现在当我将粘贴复制到 spark shell 时它不起作用,我必须在里面逐行复制。 我应该如何复制shell中的所有程序? 谢谢。 最
抱歉我的英语不好。 我想找出大量线性方程的下包络线。这映射到在其双平面中找到上(凸)壳的问题。 据我调查,有几种方法可以找到上层船体,但它们仅适用于 2-3 维。 但是,我的数据是高维的,有可用的库来
我在 Linux 上运行 spark-1.6.1,当我将 spark-env.sh 中的 SPARK_DRIVER_MEMORY 设置为 4g 并运行 spark-shell 时,我得到了这个: In
我是一名优秀的程序员,十分优秀!