数据类型到 ByteString
我有一个 newtype
我想保存在一个文件中,如下所示:
type Index = (Int, Int)
newtype Board a = Board { unboard :: Array Index a }
所以基本上是一个 Array
。但也许有一天我想添加一些其他数据,如下所示:
data BoardWithInfo a = BWI {
bwiBoard :: Board a,
bwiRef :: String,
bwiStart :: Index
}
等等。我只是想知道,是否有任何方便的、优化的函数来执行此操作,从 Array 到 ByteString 以及组合数据以及相反的方式。或者如果没有的话如何写我自己的。
I have a newtype
I'd like to save in a file, something like this:
type Index = (Int, Int)
newtype Board a = Board { unboard :: Array Index a }
So basically an Array
. But maybe I want to add some other data one day like this:
data BoardWithInfo a = BWI {
bwiBoard :: Board a,
bwiRef :: String,
bwiStart :: Index
}
And so on. I just want to know, are there any convenient, optimised functions to do this, Array
to ByteString
and combined data - and the other way around. Or how to write my own, if there are not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
数据。 Binary
带有几个实例来包装您的Board
和BoardWithInfo
类型:You'll want to use
Data.Binary
with a couple instances to wrap yourBoard
andBoardWithInfo
types:您可以派生一个 Show 实例并将其保存,或者检查来自 hackage 的二进制模块。 IIRC 它有数组实例。您需要为新类型创建实例,但由于它只是一个包装器,因此这是理所当然的。二进制模块有很好的文档和很多例子。
You could derive a Show instance and save it as such, or check the binary module from hackage. IIRC it has instance for Arrays. You need to create your instance for your newtype, but as it's just a wrapper, it's a no-brainer. The binary module has excellent documentation with lots of examples.