【发布时间】:2015-06-03 01:10:52
【问题描述】:
键值数据库的基本功能是store、fetch和delete。我正在尝试编写一个类型类,允许这些函数的返回类型是纯的或 IO,这样我就可以编写一个适用于内存中 Data.Map 支持的实现和 on -磁盘实现。
pure 类型类可能看起来像这样:
class PureQueryable d where
fetch :: d -> Key -> Maybe Value
insert :: d -> Key -> Value -> d
delete :: d -> Key -> Maybe d
...而IO 类型类可能看起来像这样:
class IOQueryable d
fetch :: d -> Key -> IO (Maybe Value)
insert :: d -> Key -> Value -> IO d
delete :: d -> Key -> IO (Maybe d)
我正在尝试将这两者结合起来,正如某人 (Vektorweg1) 在#haskell 上所建议的那样,我写了这个:
class Queryable d r where
fetch :: d -> Key -> ???
insert :: d -> Key -> Value -> ???
delete :: d -> Key -> ???
我不确定要写什么来代替??? 才能使它起作用。有什么想法吗?
【问题讨论】:
-
你为什么要开设这门课?这不是 Haskell 中适合的问题类。
-
您还有什么建议吗?
-
我的第一个问题是:你能从中得到什么?为了使抽象有意义,您需要能够高效地编写与您正在使用的实现无关的代码。您可以编写哪些高效的代码适用于任何实现?