数据类型到 ByteString

发布于 2024-11-01 01:22:35 字数 429 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-11-08 01:22:36

您需要使用数据。 Binary 带有几个实例来包装您的 BoardBoardWithInfo 类型:

import Control.Monad
import Data.Array
import Data.Binary

type Index = (Int, Int)

newtype Board a = Board { unboard :: Array Index a }
                deriving (Eq, Show)

instance (Binary a) => Binary (Board a) where
  get = liftM Board get
  put b = put (unboard b)

data BoardWithInfo a = BWI { bwiBoard :: Board a
                           , bwiRef :: String
                           , bwiStart :: Index }
                     deriving (Eq, Show)

instance (Binary a) => Binary (BoardWithInfo a) where
  get = liftM3 BWI get get get
  put b = do
    put (bwiBoard b)
    put (bwiRef b)
    put (bwiStart b)

testBoard :: Board Int    
testBoard = Board $ listArray ((1,1),(10,10)) [1..100]

testBWI :: BoardWithInfo Int
testBWI = BWI testBoard "test" (1,1)

-- returns True since the data survives encoding/decoding!
testBinaryRoundtrip = testBWI == testBWI'
  where
    testBWI' = decode $ encode testBWI

You'll want to use Data.Binary with a couple instances to wrap your Board and BoardWithInfo types:

import Control.Monad
import Data.Array
import Data.Binary

type Index = (Int, Int)

newtype Board a = Board { unboard :: Array Index a }
                deriving (Eq, Show)

instance (Binary a) => Binary (Board a) where
  get = liftM Board get
  put b = put (unboard b)

data BoardWithInfo a = BWI { bwiBoard :: Board a
                           , bwiRef :: String
                           , bwiStart :: Index }
                     deriving (Eq, Show)

instance (Binary a) => Binary (BoardWithInfo a) where
  get = liftM3 BWI get get get
  put b = do
    put (bwiBoard b)
    put (bwiRef b)
    put (bwiStart b)

testBoard :: Board Int    
testBoard = Board $ listArray ((1,1),(10,10)) [1..100]

testBWI :: BoardWithInfo Int
testBWI = BWI testBoard "test" (1,1)

-- returns True since the data survives encoding/decoding!
testBinaryRoundtrip = testBWI == testBWI'
  where
    testBWI' = decode $ encode testBWI
零度° 2024-11-08 01:22:36

您可以派生一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文