Questions tagged [reader-monad]

75 questions
5
votes
1 answer

Reader and MonadReader

Why is there a Reader monad and a MonadReader monad in Control.Monad.Reader? The package documentation talks about the Reader monad and then launches into the MonadReader documentation directly with no explanation. What is the difference between…
andro
  • 901
  • 9
  • 20
5
votes
1 answer

I know how to use it but I don't understand exactly how it does it (Reader monad)

Consider the following code (with obvious parts left out) main = do let s = "123456"; let len = runReader calculateContentLength s putStrLn $ "Original 's' length: " ++ (show len) calculateContentLength :: Reader String…
David
  • 5,991
  • 5
  • 33
  • 39
4
votes
1 answer

Confusing ReaderT definition

As an exercise I've been reimplementing some common monads and their corresponding transformers; here are some of the types I've defined: newtype Writer w a = Writer { runWriter :: (w, a) } newtype WriterT w m a = WriterT { runWriterT :: m…
4
votes
1 answer

Composition of Readers for Dependency Injection in Scala

Here is a simple service example, whose methods return reader: trait Service1_1{ def s1f1:Reader[Map[String,Int],Int] = Reader(_("name")) def s1f2:Reader[Map[String,Int],Int] = Reader(_("age")) } Here is a service-consumer, that…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
4
votes
2 answers

Why to define the constructor parameter of Reader as a function?

When learning the Reader Monad, I find that it is defined as: newtype Reader r a = Reader { runReader :: r -> a } instance Monad (Reader r) where return a = Reader $ \_ -> a m >>= k = Reader $ \r -> runReader (k (runReader m r)) r I want to…
hliu
  • 1,015
  • 8
  • 16
4
votes
2 answers

Scalaz: how does `scalaz.syntax.applicative._` works its magic

This question is related to this one, where I was trying to understand how to use the reader monad in Scala. In the answer the autor uses the following code for getting an instance of ReaderInt[String]: import scalaz.syntax.applicative._ val…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
4
votes
1 answer

Reader monad in Scala: return, local, and sequence

I'm using the Reader monad in Scala as provided by the scalaz library. I'm familiar with this monad as defined in Haskell. The problem is that I cannot find the functions equivalent to return, local, and sequence (among others). Currently I use…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
3
votes
1 answer

The Reader Monad in Kotlin with arrow-kt

I working on a medium-sized Kotlin project where I need to thread configuration information read from file through many nested calls of pure functions. This seems to be an obvious case for the Reader monad. However, I have not figured out how to…
Ulrich Schuster
  • 1,670
  • 15
  • 24
3
votes
1 answer

Reader monad - reader vs asks function difference?

There is a asks function for reader monad, which defined exactly as reader function, why it exists as a separate function with a definition the same as a reader? why not always use reader? class Monad m => MonadReader r m | m -> r where -- |…
Evg
  • 2,978
  • 5
  • 43
  • 58
3
votes
1 answer

What is "a function that you call" and what is "a function that call you"?

I'm trying to understand what is Haskell Reader monad, but I struggle with this part in the book: The “read-only” nature of the type argument r means that you can swap in a different type or value of r for functions that you call, but not for…
kirill fedorov
  • 401
  • 2
  • 6
3
votes
2 answers

How to wrap monadic action in IO

I am trying to treat a ReaderT X IO monad as IO to achieve the following: -- this is the monad I defined: type Game = ReaderT State IO …
Markus Rother
  • 414
  • 3
  • 10
3
votes
0 answers

Scala: Dependency Injection via Reader vs parameter list

There are some options to inject dependencies in FP. I want to compare here only the two: Injection via parameter list Injection via Reader The 2nd case is more composable and less verbose, in case I invoke several methods with dependencies. It…
3
votes
2 answers

Create ReaderT[F, D, A] from F[A]

type MapReaderOrOption[A] = ReaderT[Option, Map[String,String], A] I can create it from ReaderT.apply: def f:MapReaderOrOption[Int] = ReaderT(_ => Option(10)) From A type via type enrichment and pure method: import cats.Applicative import…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
3
votes
1 answer

Scala-cats: reader-composition

import cats.data.ReaderT import cats.instances.either._ trait Service1 trait Service2 case class Cats(name:String) type FailFast[A] = Either[List[String], A] type Env = (Service1, Service2, Cats) type ReaderEnvFF[A] = ReaderT[FailFast, Env,…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
3
votes
1 answer

Scala: Dependency Injection via Reader and compatibility

When we implement DI via Reader, we make a dependency a part of our method signature. Assume we have (without implementations): trait Service1 { def f1:Int = ??? } trait Service2 { def f2:Reader[Service1, Int] = ??? } type Env= (Service1,…
Alexandr
  • 9,213
  • 12
  • 62
  • 102