Questions tagged [reader-monad]

75 questions
148
votes
4 answers

What is the purpose of the reader monad?

The reader monad is so complex and seems to be useless. In an imperative language like Java or C++, there is no equivalent concept for the reader monad, if I am not mistaken. Can you give me a simple example and clear this up a little bit?
chipbk10
  • 5,783
  • 12
  • 49
  • 85
33
votes
1 answer

Configuration data in Scala -- should I use the Reader monad?

How do I create a properly functional configurable object in Scala? I have watched Tony Morris' video on the Reader monad and I'm still unable to connect the dots. I have a hard-coded list of Client objects: class Client(name : String, age : Int){…
Larry OBrien
  • 8,484
  • 1
  • 41
  • 75
19
votes
1 answer

Is it just a coincidence that Kleisli, ReaderT, and Reader are the same in Scalaz

In Scalaz Kleisli[F, A, B] is a wrapper for A => F[B]. ReaderT[F, A, B] -- reader monad transformer -- is just an alias of Kleisli[F, A, B]. Reader[A, B] monad is a specialization of ReaderT with identity monad Id: type Reader[A, B] =…
Michael
  • 41,026
  • 70
  • 193
  • 341
13
votes
3 answers

Understanding the Reader monad

I'm reading PureScript by Example and got to the part introducing the Reader monad. The example goes like this: createUser :: Reader Permissions (Maybe User) createUser = do permissions <- ask if hasPermission "admin" permissions then map…
kaqqao
  • 12,984
  • 10
  • 64
  • 118
11
votes
1 answer

How to avoid stair-stepping with Monad Transformers in scala?

I have the following code that uses the Reader monad for configuration and also has to deal with IO[Option[String]] and I've ended up with code that stair-steps in my encode function. How can I formulate a monad transformer for Reader and OptionT to…
cwmyers
  • 183
  • 7
8
votes
2 answers

Does a Powerset-over-Reader monad exist?

The canonical 'Monad instance' for environment sharing plus nondeterminism is as follows (using pseudo-Haskell, since Haskell's Data.Set isn't, of course, monadic): eta :: a -> r -> {a} -- '{a}' means the type of a set of a's eta x = \r -> {x} bind…
SEC
  • 799
  • 4
  • 16
7
votes
2 answers

What can the Reader monad do that applicative functions cannot?

Having read http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors , I can provide an example of the use of functions as applicative functors: Let's say res is a function of 4 arguments and fa, fb, fc, fd are all…
user4385532
7
votes
3 answers

One more time...can I have an example of state monad that does what I want?

I'm trying to understand the actual need for the reader and/or state monads. All the examples I've seen (including many on stackoverflow as I've hunted for suitable examples I can use and in various books and blog articles) are of the form (pseudo…
David
  • 5,991
  • 5
  • 33
  • 39
7
votes
2 answers

Is it possible to do the Reader Monad from Haskell in Clojure?

I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey, Konrad Hinsen and Leonardo Borges. The only reference I can find to the Reader Monad in Clojure is this google groups…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
7
votes
2 answers

Function composition hint

just looking for an explanation how does following composition work: (=<<) . return where (=<<) :: (a -> m b) -> m a -> m b return :: a -> m a (.) :: (b -> c) -> (a -> b) -> a -> c The final type: GHCi> :t (=<<) . return (=<<) . return ::…
6
votes
1 answer

Is there any significant difference between StateT over Reader and ReaderT over State?

When I design my programming model I always have a dilemma which approach is better: type MyMonad1 = StateT MyState (Reader Env) type MyMonad2 = ReaderT Env (State MyState) What are the benefits and trade offs between using one monad stack over…
radrow
  • 6,419
  • 4
  • 26
  • 53
6
votes
1 answer

Modifying inner reader in a transformer stack

I'm pulling together code from a number of different places, and I'm trying to deal with the following: Problem I have a transformer stack with the following simplified type: action :: m (ReaderT r IO) a and I'm trying to use the action in the…
OllieB
  • 1,431
  • 9
  • 14
6
votes
2 answers

Haskell - depth for each node in binary tree using Reader monad

I wrote the following code. It is working and using the Reader monad. Could you give me some hints about code style in Haskell ? Mainly, I mean monads -- I am newbie. import Control.Monad.Reader data Tree a = Node a (Tree a) (Tree a) …
user6023611
6
votes
1 answer

Refactoring a Haskell function that uses the Reader monad

I've got some code that looks sort of like this, ignoring all the code that isn't relevant to my question: import qualified Control.Monad.Reader as Reader data FooEnv = FooEnv { bar :: Int -> Int } type FooReader = Reader.Reader FooEnv foo :: Int…
arussell84
  • 2,443
  • 17
  • 18
5
votes
1 answer

f, g, h :: Kleisli ((->) e) a b <=> f >>> (g &&& h) = (f >>> g) &&& (f >>> h)?

Edit: We will call an arrow p pure if exists such function f that: p = arr f. I'm trying to get a better grasp of Arrows in Haskell, and I want to figure out when f >>> (g &&& h) = (f >>> g) &&& (f >>> h), where f, g, h are arrows. Obviously, it…
Zhiltsoff Igor
  • 1,812
  • 8
  • 24
1
2 3 4 5