we know that we have lists, where we have used extensivly in many a occasion, but we how about we process the files because list is essentailly just a promise of a list, and so on. So you can think of lists as promises that the next element will be delivered once it really has to and along with it, the promise of the element after it. It doesn't take a big mental leap to conclude that processing a simple list of numbers as a series of promises might not be the most efficient thing in the world.
That overhead doesn't bother us so much most of the time, but it turns out to be a liability when reading big files and manipulating them. That's why Haskell has bytestrings. Bytestrings are sort of like lists, only each element is one byte (or 8 bits) in size. The way they handle laziness is also different.
One strict bytestring resides in Data.ByteString, and they do away with the laziness completely. and the other resides on the Data.ByteString.Lazy. They're lazy, but not quite as lazy as lists.
There are no promises involved; a strict bytestring represents a series of bytes in an array. You can't have things like infinite strict bytestrings. If you evaluate the first byte of a strict bytestring, you have to evaluate it whole. The upside is that there's less overhead because there are no thunks (the technical term for promise) involved. The downside is that they're likely to fill your memory up faster because they're read into memory at once.
there are as many thunks in a list as there are elements. That's what makes them kind of slow for some purposes. Lazy bytestrings take a different approach — they are stored in chunks (not to be confused with thunks!), each chunk has a size of 64K. So if you evaluate a byte in a lazy bytestring (by printing it or something), the first 64K will be evaluated. After that, it's just a promise for the rest of the chunks. Lazy bytestrings are kind of like lists of strict bytestrings with a size of 64K. When you process a file with lazy bytestrings, it will be read chunk by chunk. This is cool because it won't cause the memory usage to skyrocket and the 64K probably fits neatly into your CPU's L2 cache.
in this post, we are going to examine the following.
- ByteStrings
- the pack function and the unpack function (e..g packpack :: [Word8] -> ByteString get a list of bytes of word8, and return a string)
- fromChunks and toChunks (takes a list of strict bytestrings and converts it to a lazy bytestring.takes a lazy bytestring and converts it to a list of strict ones.)
- cons (lazy one) and the strict one (cons')
- byteString module has limited functions limited to Data.List
- Last we will present you a example of copying file with the ByteString modules.
-- file -- bytestring_io.hs -- description: -- bytestring is sorts of like lists, only each elements is one byte (or 8 bits) in size import qualified Data.ByteString.Lazy as B import qualified Data.ByteString as S -- pack function has the following signature -- ghci> B.pack [99,97,110] -- Chunk "can" Empty -- ghci> B.pack [98..120] -- Chunk "bcdefghijklmnopqrstuvwx" Empty -- unpack is the inverse function of pack, it takes a bytestring and turns it to a list bytes -- fromChunks -- taks a list of stric bytestrings and converts it lazily bytestring. -- toChunks -- takes a lazy bytestring and converts it to a list of strict ones. -- ghci> B.fromChunks [S.pack [40,41,42], S.pack [43,44,45], S.pack [46,47,48]] -- Chunk "()*" (Chunk "+,-" (Chunk "./0" Empty)) -- bytestring version of ':' is called cons -- and cons' is an optimized version of cons -- ghci> B.cons 85 $ B.pack [80,81,82,84] -- Chunk "U" (Chunk "PQRT" Empty) -- ghci> B.cons' 85 $ B.pack [80,81,82,84] -- Chunk "UPQRT" Empty -- ghci> foldr B.cons B.empty [50..60] -- Chunk "2" (Chunk "3" (Chunk "4" (Chunk "5" (Chunk "6" (Chunk "7" (Chunk "8" (Chunk "9" (Chunk ":" (Chunk ";" (Chunk "<" -- Empty)))))))))) -- ghci> foldr B.cons' B.empty [50..60] -- As you can see empty makes an empty bytestring. -- empty [50 .. 60]
and followed by the copying file examples.
-- file -- copyfile_io.hs -- description: -- implementing our own version of copy file import System.Environment import qualified Data.ByteString.Lazy as B main = do (fileName1:fileName2:_) <- getArgs copyFile fileName1 fileName2 copyFile :: FilePath -> FilePath -> IO () copyFile source dest = do contents <- B.readFile source B.writeFile dest contents
相关推荐
Atom-haskell-ghc-mod.zip,haskell-ghc-mod atom packagehaskell ghc mod atom包,atom是一个用web技术构建的开源文本编辑器。
haskell-mode emacs haskell-mode emacs
Atom-ide-haskell-hoogle.zip,在光标下显示符号的滚动信息艾德·哈斯克尔·胡格尔,atom是一个用web技术构建的开源文本编辑器。
Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...
Programming-in-Haskell-2nd-Edition.pdf
从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...
haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...
Get Programming with HASKELL-2018-英文版
用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...
演示医疗用例的参考DAML应用程序 -Haskell-TypeScript-下载
haskell-chart, haskell的2D 图表库 图 haskell的2D 图表库进一步的信息可以在关联的 wiki中找到。
你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...
Server Metaprogramming Ruby-Pyton-Groovy-Haskell-Erlang.pdf
A History of Haskell - Being Lazy With Class
haskell-formatter --force --input a.hs --output a.hs hindent a.hs 就地格式化多个文件 hindent a.hs b.hs 将stdin格式化为stdout haskell-formatter hindent 订单进口 完成 hindent --sort-imports … ...
Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。
haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...
Atom-atom-haskell-scry.zip,De-emphasize qualified Haskell identifiers.SCRY,atom是一个用web技术构建的开源文本编辑器。
Atom-atom-haskell-pointfree.zip,atom包:将选择转换为无点或有点表示Haskell无点包,atom是一个用web技术构建的开源文本编辑器。
安装过程中应在路径上放置一个名为haskell-in-haskell的可执行文件。 否则,您可以使用cabal run haskell-in-haskell --直接运行项目,然后输入要传递的参数。 编译中 要编译Haskell文件,只需运行: haskell-in-...