为什么Haskell Data.ByteString.Lazy.char8没有解压功能?
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
returns我一对[Char]
而不是一对Bytestring
。
非懒人版有returns一对Bytestring
的解压功能:
https://hackage.haskell.org/package/bytestring-0.10.8.1/docs/Data-ByteString-Char8.html#v:unzip
为什么懒人版没有类似的功能?
(请随意提出更好的解决方案来解决我的共同修剪问题。我想要一些不太慢的东西。)
乍一看,惰性字节串的unzip
归结为在一对"byte"列表的两半上使用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')
... 不会 运行 在常量内存中。 (顺便说一句,jberryman 提到的现有 Data.ByteString.Lazy.unzip
也是如此。)
P.S.: 无论如何,我不确定在这种情况下惰性字节串是否真的让你受益匪浅,因为 last . C.group
从一开始就将整个第一个字节串带入内存。
我想从字符串中删除尾随字符 '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
returns我一对[Char]
而不是一对Bytestring
。
非懒人版有returns一对Bytestring
的解压功能:
https://hackage.haskell.org/package/bytestring-0.10.8.1/docs/Data-ByteString-Char8.html#v:unzip
为什么懒人版没有类似的功能?
(请随意提出更好的解决方案来解决我的共同修剪问题。我想要一些不太慢的东西。)
乍一看,惰性字节串的unzip
归结为在一对"byte"列表的两半上使用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')
... 不会 运行 在常量内存中。 (顺便说一句,jberryman 提到的现有 Data.ByteString.Lazy.unzip
也是如此。)
P.S.: 无论如何,我不确定在这种情况下惰性字节串是否真的让你受益匪浅,因为 last . C.group
从一开始就将整个第一个字节串带入内存。