[英]Haskell, MongoDB, eval JavaScript
I wrote a method to execute JavaScript code by using eval
method in Haskell driver for MongoDB. 我编写了一种在MongoDB的Haskell驱动程序中使用
eval
方法执行JavaScript代码的方法。
toolsDB_GenerateID :: Action IO Value
toolsDB_GenerateID =
eval (Javascript ([] :: [Field]) "var ret_id = db.counters.findAndModify({query: { _id: \'my_id\' },update: { $inc: { seq: 1 } },new: true}); return {id:ret_id.seq};")::Action IO Value
It works!!! 有用!!!
I use that in the following: 我在以下使用它:
inserData :: Action IO ()
inserData = do resultEval <-toolsDB_GenerateID
insert "test" ["id" =: resultEval]
liftIO $ return ()
I just can not understand how I can get a real value from Action IO Value
? 我只是不明白如何从
Action IO Value
获得真正的Action IO Value
?
Like this: 像这样:
Action IO Value -> Value
or 要么
Action IO Value -> Int
How can I release that? 我该如何释放?
You can't do what you ask, but chances are you do not need to. 您不能做自己想做的事,但是您不一定需要做。 You can do the following, instead
您可以执行以下操作
foo :: Action IO SomeOtherType
foo = do value <- action -- where action :: Action IO SomeType
-- here value :: SomeType can be used normally
...
lastAction
with the only restriction that the lastAction
has type Action IO SomeOtherType
. 唯一的限制是
lastAction
类型为Action IO SomeOtherType
。
The thumb rule is, you can't extract a value from a monad forever, but you can extract it "temporarily" as long as you eventually produce another value inside the same monad. 经验法则是,您不能永远从monad中提取值,但是只要最终在同一monad中产生另一个值,就可以“临时”提取它。 This is (arguably) what monads are all about, from a purely practical point of view.
从纯粹的实践角度来看,这就是(可能是)单子论的全部。
I'd suggest you read some monad tutorial. 我建议您阅读一些monad教程。 Monads in pictures is among the easiest, and one of my favourites.
图片中的Monad最简单,也是我的最爱之一。 The one in LYAH is also good, and informative.
LYAH中的一个也不错,而且内容丰富。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.