Questions tagged [reader-monad]
75 questions
3
votes
1 answer
How to inject dependencies through Scala Reader from Java code
Here is a dependency service:
public class Service1 {}
Scala code that uses it via reader:
object TupleEx {
type FailFast[A] = Either[List[String], A]
type Env[A] = ReaderT[FailFast, Service1, A]
import cats.syntax.applicative._
import…

Alexandr
- 9,213
- 12
- 62
- 102
3
votes
1 answer
Preferring Reader monad over directly passing the environment as parameters
I was writing a basic CRUD app in Haskell, using libraries Servant and Opaleye.
Servant to setup the API endpoints and Opaleye to store the data in DB.
Let's say there's an endpoint GET /users which returns the list of all users from DB and another…

kishlaya
- 453
- 2
- 14
3
votes
2 answers
How to see code of Monad Reader?
I am a beginner in Haskell, and I want to understand Reader Monad. I know how to use this monad. But I want to see the implementation of monad (particularly code of function "return"). How can I see this code?

Alex Aparin
- 4,393
- 5
- 25
- 51
3
votes
2 answers
Recursively walking an AST using foldl and the Reader monad without boilerplate
I'm traversing an AST using simple pattern matching and the Reader monad.
Elsewhere in my project I've defined a walk function for traversing the AST, which at its heart uses foldl to reduce the results of visiting each node in the tree into a…

Greg Hurrell
- 5,177
- 23
- 27
2
votes
1 answer
The reader monad for dependency injection
I am a dev trying to bend my mind around the typelevel stack (Cats and Cats-Effect)after years of experience with less functional scala.
I'm having a real trouble bending my head around the Reader monad. I can somewhat follow the examples here:…

Emil D
- 1,864
- 4
- 23
- 40
2
votes
0 answers
Interpreting the `Reader` trait in "Simplicitly"
In "Simplicitly: foundations and applications of implicit function types", Odersky et al. briefly introduce the Reader monad, just to replace it with a superior alternative one paragraph later. This is what the given definition looks like:
trait…

Andrey Tyukin
- 43,673
- 4
- 57
- 93
2
votes
3 answers
Is it possible to implement this generic flip?
I would like to write an object with the type signature:
genericFlip ::
( MonadReader (o (n c)) m
, MonadReader a n
, MonadReader b o
)
=> m (n (o c))
That is essentially a flip for monad readers.
Now it is easy enough to write the…

Wheat Wizard
- 3,982
- 14
- 34
2
votes
1 answer
Scala: for-comprehension with guard inside reader
Here is code example:
type FailFast[A] = Either[List[String], A]
import cats.instances.either._
def f1:ReaderT[FailFast, Map[String,String], Boolean] = ReaderT(_ => Right(true))
def f2:ReaderT[FailFast, Map[String,String], Boolean] =…

Alexandr
- 9,213
- 12
- 62
- 102
2
votes
1 answer
Implementing the Reader monad (from Real World Haskell book)
newtype Reader e a = R { runReader :: e -> a }
instance Monad (Reader e) where
return a = R $ \_ -> a
m >>= k = R $ \r -> runReader (k (runReader m r)) r
I am having difficulty understanding these two snippets. I can tell that the first one…

金允雄Oliver
- 229
- 2
- 13
2
votes
2 answers
Reader Monad - explanation of trivial case
I have been trying to get to grips with the reader monad and came across this tutorial. In it, the author presents this example:
example2 :: String -> String
example2 context = runReader (greet "James" >>= end) context
where
greet ::…

matt
- 1,817
- 14
- 35
2
votes
3 answers
Reader Monad clarification
I am trying to make sense of the reader monad but can't seem to grasp what bind (>>=) does in this monad.
Here's the implementation I am analyzing:
newtype Reader e a = Reader { runReader :: (e -> a) }
instance Monad (Reader e) where
return a …

oskar132
- 794
- 2
- 12
- 32
2
votes
1 answer
How to use the Reader Monad with (Int -> Int)?
I would like to learn, how to use the Reader Monad. Unfortunately only a small amount of example code is available
I would like to create a Reader, where the environment and the retrived values are Int. I defined the type like this:
type IntRead =…

Iter Ator
- 8,226
- 20
- 73
- 164
2
votes
1 answer
Reader monad - how does it conform to Monad interface?
I'm learning category theory.
I understand the concept of reader monad, it's even pretty easy to implement:
case class Reader[DEP, A](g: DEP => A) {
def apply(dep: DEP): A = g(dep)
def map[B](f: A => B): Reader[DEP, B] = Reader(dep =>…

slnowak
- 1,839
- 3
- 23
- 37
2
votes
1 answer
How is the below repository being threaded (passed) into each function call?
I was sure the below underscores are unit meaning function return value is ignored. (The below is taken from book functional and reactive domain modeling). If that's correct then I have a question about the below code:
case class…

Jas
- 14,493
- 27
- 97
- 148
2
votes
1 answer
Reader Monad in Purescript
I was playing with the Reader monad in Purescript and I encountered a weird behaviour. I don't know if it's because my lack of comprehension of this monad or if I'm missing something else.
This is my code :
type Level = Number
type Doc = Reader…

ThomasC__
- 311
- 3
- 9