reader-monad

Reader monad - how does it conform to Monad interface?

元气小坏坏 提交于 2020-01-03 02:10:14
问题 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 => f(apply(dep))) def flatMap[B](f: A => Reader[DEP, B]): Reader[DEP, B] = Reader(dep => f(apply(dep)) apply dep) } However, I have problems implementing it with constraint to some generic Monad interface, i.e trait Monad[A] { def pure(a: A): Monad[A] def map[B](f:

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

…衆ロ難τιáo~ 提交于 2019-12-23 12:07:58
问题 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 known why using function as constructor parameter instead of something else such as a tuple: newtype Reader r a = Reader { runReader :: (r, a) } instance Monad (Reader r) where -- Here I cannot get r when defining return function, -- so does that's the

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

别等时光非礼了梦想. 提交于 2019-12-19 02:32:40
问题 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 discussion. My question is: Is it possible to do the Reader Monad from Haskell in Clojure? Could you provide an example? 回答1: Sure. A Reader is just a function that takes an environment and extracts some value from it. With Reader , m-result takes some value

Reader monad in Scala: return, local, and sequence

a 夏天 提交于 2019-12-13 13:27:03
问题 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 constructs that I do not like since I'm repeating myself or making my code a bit obscure. Regarding return , I'm currently using: Reader{_ => someValue} I'd rather just use a construct like unit(someValue) , but I could not find anything on the

How to inject dependencies through Scala Reader from Java code

北城以北 提交于 2019-12-12 16:16:56
问题 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 cats.instances.either._ def f:Env[Int] = 10.pure[Env] } Java test where I try to inject Service1: @Test public void testf() { Service1 s = new Service1(); TupleEx.f().run(s); } I am getting an exception: Error:(10, 16) java: method run in class cats.data

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

人走茶凉 提交于 2019-12-10 15:20:37
问题 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 alwaysHello2: ReaderInt[String] = "hello".point[ReaderInt] Which mechanisms does Scala use to resolve the type of the expression "hello".point[ReaderInt] so that it uses the right point function? 回答1: A good first step any time you're trying to figure

Does a Powerset-over-Reader monad exist?

半世苍凉 提交于 2019-12-07 00:24:58
问题 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 :: (r -> {a}) -> (a -> r -> {b}) -> r -> {b} m `bind` f = \r -> {v | x ∈ m r, v ∈ f x r} Generally, when trying to combine a 'container' monad like Powerset (List, Writer, etc) with a second monad m (here, roughly, Reader), one 'wraps' m around the

Does a Powerset-over-Reader monad exist?

▼魔方 西西 提交于 2019-12-05 03:39:55
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 :: (r -> {a}) -> (a -> r -> {b}) -> r -> {b} m `bind` f = \r -> {v | x ∈ m r, v ∈ f x r} Generally, when trying to combine a 'container' monad like Powerset (List, Writer, etc) with a second monad m (here, roughly, Reader), one 'wraps' m around the container monad, as done above. I wonder, then, about the following potential Powerset-over-Reader

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

我只是一个虾纸丫 提交于 2019-12-03 01:54:02
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] = ReaderT[Id, A, B] . Is it just a coincidence or there are some deeper reasons that Kleisli , ReaderT , and Reader are isomorphic in Scalaz ? You can think of it as arriving at the same place by two different routes. On one side you start with the reader monad, which is simply a kind of wrapper for functions. Then you realize that you want to integrate this

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

两盒软妹~` 提交于 2019-12-03 00:51:20
问题 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){ /* etc */} object Client{ //Horrible! val clients = List(Client("Bob", 20), Client("Cindy", 30)) } I want Client.clients to be determined at runtime, with the flexibility of either reading it from a properties file or from a database. In the Java