[英]how to drop lines when streaming from a file using Haskell and the turtle library
假設我想從一個文件流到另一個文件,但我想跳過輸入文件的前n行。 如果沒有先使用'fold'折疊整個第一個文件,我該怎么做?
import Turtle
main = output "/tmp/b.txt" (f (input "/tmp/a.txt"))
應該做什么才能實現這一目標?
ps:我沒有足夠的聲譽來創建'haskell-turtle'標簽。
我認為這是正確的代碼:
import Data.IORef
import Turtle
drop :: Int -> Shell a -> Shell a
drop n s = Shell (\(FoldM step begin done) -> do
ref <- newIORef 0
let step' x a = do
n' <- readIORef ref
writeIORef ref (n' + 1)
if n' < n then return x else step x a
foldIO s (FoldM step' begin done) )
...除了我可能稱之為除了drop
之外的其他東西以避免與前奏曲發生沖突。
它與Turtle.Prelude.limit
(請參閱源代碼以進行比較)。 唯一的區別是我已經顛倒了if
語句的then
和else
子句。
如果這解決了你的問題,那么我會把它添加到Turtle.Prelude
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.