monad-transformers

Lift through nested state transformers (mtl)

£可爱£侵袭症+ 提交于 2019-12-11 02:31:33
问题 So I'm working on an extensible application framework, and a key part of the framework is to be able to run state monads over many different state types; I've got it set up, and can run the nested state monads; however a key feature I need is for monads over nested states to be able to run actions over the global state as well; I managed to rig this up using some complicated Free Monads in an earlier project, but now I'm using mtl and I'm a bit stuck. Here's some context: newtype App a = App

How to compose Future of Either/Disjunction in Scala

岁酱吖の 提交于 2019-12-10 18:46:03
问题 Suppose I have the following functions to compose: val mayFail1: Int => Error \/ Int = ??? val slowAndMayFail: Int => Error \/ String = ??? val mayFail2: String => Error \/ Int = ??? val mayFail3: String => Error \/ Int = ??? val mayFail: Int => Error \/ Int = {x => for { x1 <- mayFail1(x) s <- slowAndMayFail(x1) x2 <- mayFail2(s) x3 <- mayFail3(x2) } yield x3 } The function mayFail is slow because of slowAndMayFail , so I would like it to return a future of Error \/ Int . The straightforward

How do I use list monad inside of ReaderT?

淺唱寂寞╮ 提交于 2019-12-10 17:07:06
问题 How do I use Reader / ReaderT to ask for a list type, e.g. [(Int, Int)] and then perform calculations inside the list monad (of the type that was ask ed for)? My broken code follows, shortened for clarity: attempt :: Int -> Int -> ReaderT [(Int,Int)] [] [[Int]] attempt start end = do (s0, e0) <- ask return [0] To give you an idea of what I'm trying to do, here is the same function, using the list monad but not Reader: paths :: [(Int, Int)] -> Int -> Int -> [[Int]] paths edges start end = if

Stateful loop with different types of breaks

你说的曾经没有我的故事 提交于 2019-12-10 15:08:56
问题 I am trying to convert the following stateful imperative code into Haskell. while (true) { while (get()) { if (put1()) { failImmediately(); } } if (put2()) { succeedImmediately(); } } Both the put1 and put2 read a state of the system and modify it. get can for simplicity just read the state. failImmediately should break out of the endless-loop and present one type of result, succeedImmediately should also break out but present a different result. What I tried to use was State Env Result where

Why is there a MonadMask instance for ExceptT?

六眼飞鱼酱① 提交于 2019-12-10 13:19:55
问题 In a previous question, I asked Why is there no MonadMask instance for ExceptT? and got a number of compelling answers, including from the library author, why there cannot exist a lawful instance. As of February 2018, the exceptions library does now have an instance of MonadMask for ExceptT. So now I'm asking the exact opposite question: Why are the answers to my previous question incorrect? 回答1: The documentation of the exceptions library has the answer in its docs: This method was added in

creating MonadBaseControl instance for newtype

不打扰是莪最后的温柔 提交于 2019-12-10 12:31:42
问题 Suppose I have simple newtype declaration newtype Foo a = Foo { unFoo :: ReaderT Int IO a } I want to make Foo instance of MonadBaseControl IO. It should be easy, since ReaderT Int IO is already an instance of MonadBaseControl IO. However, automatically deriving it using GeneralizedNewtypeDeriving doesn't work, because MonadBaseControl class has an associated type. How can one write a MonadBaseControl IO instance for Foo? defaultLiftBaseWith and defaultRestoreM should be helpful, but it's a

Is this a valid monad transformer in Javascript?

左心房为你撑大大i 提交于 2019-12-09 00:13:14
问题 In order to better understand monad transformers I implemented one. Since Javascript is dynamically typed I don't mimic type or data constructors but declare only plain old Javascript objects, which hold the corresponding static functions to form a specific monad / transformer. The underlying idea is to apply these methods to a value/values in a container type. Types and containers are separated so to speak. Array s can contain any number of elements. It is trivial to extend Array s so that

Is there a principled way to compose two monad transformers if they are of different type, but their underlying monad is of the same type?

只愿长相守 提交于 2019-12-08 17:43:49
问题 Not much I can do to expand the question. But here is a use case: let's say you have two monad transformers, t and s , transforming over the same monad m : master :: (MonadTrans t, Monad m) => t m a b slave :: (MonadTrans t, Monad m) => s m a b And I want to compose master and slave such that they can communicate with each other when m primitives are lifted into t and s . The signature might be: bound :: (MonadTrans t, MonadTrans s, Monad m, Monoid a) => t m a b -> s m a b -> (...) But what

Convert Railway oriented failure track to Rx friendly errors

非 Y 不嫁゛ 提交于 2019-12-08 01:52:39
问题 I'm using a library that takes the results as two-track values (success and failure).In the Observable.map function bodies I often get an observable from success track of a function, and I don't know how to handle them (at Observable.map body). In the other words I often get stuck in situations that the result looks something like the following (of course it's the simplest one): Rop.Result<IObservable<Rop.Result<IObservable,Messages>,Messages> On one hand, translating messages back to

Scotty Using MongoDB

心不动则不痛 提交于 2019-12-07 09:56:35
问题 I'm relatively new to Haskell, and this is my first time working with monad transformers. I'd really appreciate some help. runQuery :: Pipe -> Query -> ActionM (Either Failure [Document]) runQuery pipe query = access pipe master "nutrition" (find query >>= rest) main = do pipe <- runIOE $ connect $ host "127.0.0.1" scotty 3000 $ do post "/" $ do b <- body let user :: Either String User = eitherDecode b case user of Left err -> text . pack $ "Could not decode the user:" ++ err ++ ":\n" ++