Reputation: 577
I wrote a method to execute JavaScript code by using eval
method in Haskell driver for MongoDB.
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
?
Like this:
Action IO Value -> Value
or
Action IO Value -> Int
How can I release that?
Upvotes: 1
Views: 114
Reputation: 116174
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
.
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. This is (arguably) what monads are all about, from a purely practical point of view.
I'd suggest you read some monad tutorial. Monads in pictures is among the easiest, and one of my favourites. The one in LYAH is also good, and informative.
Upvotes: 3