- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想在 hedis 之上写一个简单的 DSL , 一个 redis 库。目标是编写如下函数:
iWantThis :: ByteString -> MyRedis ()
iWantThis bs = do
load bs -- :: MyRedis () It fetches a BS from Redis and puts it as
-- the state in a state monad
bs <- get -- :: MyRedis (Maybe ByteString) Gets the current state
put $ doSomethingPure bs -- :: MyMonad () Updates the state
commit -- :: MyRedis () Write to redis
基本思想是从 Redis 中获取数据,将其放入状态 monad 中,对状态进行一些处理,然后将更新后的状态放回 Redis。
显然,它应该是原子的,所以 load
和 put
应该发生在同一个 Redis 事务中。 Hedis 通过将对 Redis
的调用包装在 RedisTx (Queued a)
中来实现这一点。例如,我们有 get::ByteString -> RedisTx (Queued a)
。Queued
是一个 monad,然后您在 Queued a
上运行 multiExec
以执行 Queued a
中的所有内容同样的交易。所以我尝试这样定义我的 MyRedis
:
import qualified Database.Redis as R
newtype MyRedis a = MyRedis { runMyRedis :: StateT MyState R.RedisTx a } -- deriving MonadState, MyState...
run
函数调用 multiExec
所以我确信只要我留在 MyRedis
中,所有事情都发生在同一个事务中。
run :: MyRedis (R.Queued a) -> MyState -> IO (R.TxResult a)
run m s = R.runRedis (undefined :: R.Connection) (R.multiExec r)
where r = evalStateT (runMyRedis m) s
此外,我可以将 commit
定义为:
commit :: ByteString -> MyRedis (R.Queued R.Status)
commit bs = do
MyState new <- get
(MyRedis . lift) (R.set bs new)
计算
看起来像:
computation :: MyRedis (R.Queued R.Status)
computation = do
load gid
MyState bs <- get
put $ MyState (reverse bs)
commit gid
where gid = "123"
但是我想不出“load”怎么写
load :: ByteString -> MyRedis ()
load gid = undefined
其实我觉得load
是不行的,因为get
的类型是ByteString -> RedisTx (Queued (Maybe ByteString))
并且我无法在不执行它的情况下查看 Queued
monad。
问题:
由于 Hedis 的 get 类型,定义具有上述语义的 load
函数没有意义是否正确?
是否可以更改 MyRedis
类型定义以使其工作?
Hedis 没有定义 RedisT
monad 转换器。如果存在这样的变压器,它会有什么帮助吗?
Hedis 定义(但不导出给 lib 用户)一个 MonadRedis
类型类;让我的 monad 成为该类型类的实例会有帮助吗?
这是正确的方法吗?我想:
MyRedis
get
和set
)您可以使用 http://pastebin.com/MRqMCr9Q 处的代码.对于 pastebin 很抱歉,lpaste.net 目前已关闭。
最佳答案
你想要的是不可能的。特别是,您不能在一个 Redis 事务中运行计算时提供 monadic 接口(interface)。与您正在使用的库无关 - 这不是 Redis 可以做的事情。
Redis 事务与您在关系数据库世界中可能习惯的 ACID 事务有很大不同。 Redis 事务具有批处理 语义,这意味着后面的命令不能以任何方式依赖于前面命令的结果。
看:这是与您的示例类似的东西,在 Redis 命令行上运行。
> set "foo" "bar"
OK
> multi
OK
> get "foo"
QUEUED -- I can't now set "baz" to the result of this command because there is no result!
> exec
1) "bar" -- I only get the result after running the whole tran
无论如何,这就是该库的有点奇怪的 Queued
类型的目的:这个想法是防止您在批处理结束之前访问批处理命令的任何结果。 (似乎作者想要抽象批处理和非批处理命令,但有更简单的方法可以做到这一点。请参阅下文了解我如何简化事务接口(interface)。)
所以当涉及 Redis 事务时,没有“选择下一步做什么”,但是 (>>=)::m a -> (a -> m b) -> m b 的整个要点
是后来的影响可能取决于早期的结果。您必须在 monad 和事务之间做出选择。
如果您决定需要事务,可以使用 Monad
的替代方法,称为 Applicative
,它可以很好地支持纯静态效果。这正是我们所需要的。这里有一些(完全未经测试的)代码说明了我将如何为您的想法制作一个应用
版本。
newtype RedisBatch a = RedisBatch (R.RedisTx (R.Queued a))
-- being a transactional batch of commands to send to redis
instance Functor RedisBatch where
fmap = liftA
instance Applicative RedisBatch where
pure x = RedisBatch (pure (pure x))
(RedisBatch rf) <*> (RedisBatch rx) = RedisBatch $ (<*>) <$> rf <*> rx
-- no monad instance
get :: ByteString -> RedisBatch (Maybe ByteString)
get key = RedisBatch $ get key
set :: ByteString -> ByteString -> RedisBatch (R.Status)
set key val = RedisBatch $ set key val
runBatch :: R.Connection -> RedisBatch a -> IO (R.TxResult a)
runBatch conn (RedisBatch x) = R.runRedis conn (R.multiExec x)
如果我想抽象出事务性或非事务性行为,就像库作者试图做的那样,我会编写第二种类型 RedisCmd
来公开一个 monadic接口(interface),以及一个包含我的原始操作的类,以及我的两个 RedisBatch
和 RedisCmd
类型的实例。
class Redis f where
get :: ByteString -> f (Maybe ByteString)
set :: ByteString -> ByteString -> f (R.Status)
现在,具有 (Applicative f, Redis f) => ...
类型的计算可以用于任何一种行为(事务性或非事务性),但那些需要 monad ( Monad m, Redis m) => ...
只能在非事务模式下运行。
总而言之,我认为这不值得。人们似乎喜欢像这样在库上构建抽象,总是提供比库少的功能,并为潜伏的错误编写更多代码。每当有人说“我可能想切换数据库”时,我叹息:唯一足够抽象的抽象purpose 是一个不提供任何功能的。担心在需要时切换数据库(也就是说,永远不要)。
另一方面,如果您的目标不是抽象数据库而只是清理界面,最好的办法可能是 fork 库。
关于haskell - 在 hedis 之上构建一个 monad,一个 haskell redis 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35610524/
Java 库和 android 库有什么区别,各自有什么优点/缺点? 最佳答案 您可以在 Android 应用程序中包含标准 Java .jar 文件库。它们在 .apk 构建时被翻译成 Dalvik
所以,我现在的代码就像从 Java 层加载库(比如 liba.so),并在内部 liba.so 加载 libb.so。因此,如果我必须将所有库打包到 APK 中并将其安装在没有 root 访问权限的设
我想在我的系统中设置 LEDA 库。 我已经从以下链接下载了 LEDA 库 http://www.algorithmic-solutions.info/free/d5.php Instruct
我想用 autoconf 创建一个共享库。但是,我希望共享库具有“.so”扩展名,而不是以“lib”开头。基本上,我想制作一个加载 dlopen 的插件。 .是否有捷径可寻? 当我尝试使用 autoc
我需要在 Apps 脚本应用程序上修改 PDF。为此,我想使用 JS 库:PDF-LIB 我的代码: eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib
我正在构建一个使用以下 Boost header 的程序(我使用的是 Microsoft Visual C++ 10), #include #include #include #include
当我通过 cygwin 在 hadoop 上运行此命令时: $bin/hadoop jar hadoop-examples-*.jar grep input output 'dfs[a-z.]+' 我
我已经通过 vcpgk 成功安装了一个 C++ 库,名为:lmdb:x64-windows 我还安装了lmdb通过 Cabal 安装的 Haskell 绑定(bind)包 在尝试测试 lmdb 包时:
我该如何解决这个问题? 我刚刚将 javacv jar 文件复制到我的项目 Lib 文件夹下,但出现了这个错误! 我可以找到这个thread来自谷歌,但不幸的是,由于我国的谷歌限制政策,该页面无法打开
我有一个 Android 库项目 FooLib。 FooLib 引用 Android Context 之类的东西,但不需要任何资源文件(res/ 中的东西)所以我目前将其打包为供我的应用使用的 JAR
我正在开发一个 Android 应用程序(使用 Android Studio),它能够通过手势识别算法了解您正在进行的 Activity 。对于我使用 nickgillian ithub 帐户上可用的
关于从 .NET Framework 项目中引用 .NET Standard 类库的问题有很多类似的问题,其中 netstandard 库中的 NuGet 包依赖项不会流向 netframework
我已经从互联网上下载了 jna-4.2.2.jar,现在想将这个 jar 导入到我的项目中。但是当我试图将这个 jar 导入我的项目时,出现以下错误。 [2016-06-20 09:35:01 - F
我正在尝试通过编译在 Mac 上安装 rsync 3.2.3。但是,我想安装所有功能。为此,它需要一些库,此处 ( https://download.samba.org/pub/rsync/INSTA
进入 Web 开发有点困难。过去 5 年我一直致力于 winforms 工作。所以我正在努力从一种切换到另一种。前段时间,我使用过 JavaScript,但现在还没有大量的 JavaScript 库
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在寻找一个用Python编写的与logstash(ruby + java)类似的工具/库。 我的目标是: 从 syslog 中解析所有系统日志 解析应用程序特定日志(apache、django、m
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我花了几天时间试图寻找用于 JavaPOS 实现的 .jar 库,但我找不到任何可以工作的东西。我找到了很多像这样的文档:http://jpos.1045706.n5.nabble.com/file/
这个问题在这里已经有了答案: Merge multiple .so shared libraries (2 个答案) 关闭 9 年前。 我有我在代码中使用的第三方库的源代码和对象。该库附带有关如何使
我是一名优秀的程序员,十分优秀!