[英]Data.ByteString.Lazy.Char8 newline conversion on Windows---is the documentation misleading?
[英]Why doesn't Haskell Data.ByteString.Lazy.Char8 have an unzip function?
我想從字符串中刪除尾隨字符 'T',同時將關聯的字符串縮短與從第一個字符串中刪除的字符數相同的字符數。
我試過如下:
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Lazy.Char8 as C
zipTrim s q
| C.null s = (C.empty, C.empty)
| C.head (last $ C.group s) == 'T' =
unzip (C.zip (C.concat $ init $ C.group s) q)
| otherwise = (s, q)
但是, unzip
返回給我一對[Char]
而不是一對Bytestring
。
非惰性版本有一個解壓縮函數,返回一對Bytestring
: https://hackage.haskell.org/package/bytestring-0.10.8.1/docs/Data-ByteString-Char8.html#v:unzip
為什么懶人版沒有類似的功能?
(隨意為我的共同修剪問題提出更好的解決方案。我想要一些不太慢的東西。)
乍一看,為惰性字節串unzip
歸結為在一對“字節”列表的兩半上使用pack
:
import Data.Bifunctor
import qualified Data.ByteString.Lazy.Char8 as C
unzipLazy :: [(Char, Char)] -> (C.ByteString, C.ByteString)
unzipLazy = bimap C.pack C.pack . unzip
不過,這可能會讓您措手不及——正如 Reid Barton 指出的那樣,“[不是] 很清楚懶惰的字節串 [是] 有多懶惰”。 例如,這...
print . fst . unzipLazy $ repeat ('a', 'b')
...不在恆定內存中運行。 (順便說Data.ByteString.Lazy.unzip
,jberryman 提到的現有Data.ByteString.Lazy.unzip
發生同樣的情況。)
PS:無論如何,我不確定懶惰的字節串在這種情況下是否真的讓你受益匪淺,就像last . C.group
last . C.group
從一開始就將整個第一個字節last . C.group
帶入內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.