如果以下是基于我的误解,我会提前道歉.我无法找到我需要的答案.
我正在实现一个程序,从文件中读取序列化对象,使用惰性评估.我正在解析该文件,首先将其作为单个Lazy ByteString读取,然后使用Get Monad解析它.在文件中,存在一个存储类型描述符的点,以及存储数据的另一个点.类型描述符告诉我如何解释数据以及它最终具有哪种类型,并且该类型可以采用嵌套形式; Double或[[Word8]]是两种可能性.
现在,我想出的这个想法(以及简单的听起来非常优雅)的想法如下:如果方法(解析类型描述符)创建 - 但不运行 - 如果稍后可以运行Get Monad 使用保存数据的ByteString?
这将需要一个像这样的方法:
parseTypeDescriptor :: Get (Get a)
Run Code Online (Sandbox Code Playgroud)
其中a与描述符描述的类型相同(意味着它可以采用嵌套形式).以下是代码的一些部分:
parseTypeDescriptor :: Get (Get a)
-- first part of the type descriptor is an id (Word8) that implies a type
parseTypeDescriptor = getWord8 >>= go
where go 0 = return getWord8
go 1 = return getWord16be
go 2 = return getWord32be
...
-- id 5 indicates that the type is an array
-- this means two more values are …Run Code Online (Sandbox Code Playgroud)