Function1和Reader Monad之间的关系 [英] Relation between Function1 and Reader Monad
问题描述
尽管我了解阅读器monad的实现,但我在下面给出了其中最突出的两种实现方法
although i understand the implementation of the reader monad of which i give 2 of the most prominent way to do it below
case class Reader[R, A](run: R => A)
def readerMonad[R] = new Monad[({type f[x] = Reader[R,x]})#f] {
def unit[A](a: => A): Reader[R, A] = Reader(_ => a)
override def flatMap[A,B](st: Reader[R, A])(f: A => Reader[R, B]): Reader[R, B] =
Reader(r => f(st.run(r)).run(r))
}
或更简单地
case class Reader[R, A](run: R => A) {
def map[B](f: A => B): Reader[R, B] =
Reader(r => f(run(r)))
def flatMap[B](f: A => Reader[R, B]): Reader[R, B] =
Reader(r => f(run(r)).run(r))
}
我想知道Reader Monad和Function1之间是否存在内在联系.我已经在这里准备好了,那里的评论暗示了这一点.根据定义,Reader是Function1 monad吗?
I wonder if there is an intrinsic relationship between Reader Monad and Function1. I have been readying here and there comment hinting at that. Is Reader by definition a Function1 monad ?
我不这么认为.但是我想在这个概念的帮助下全神贯注.
I do not think so. But i would like to wrap my head around the concept with some help.
当这些函数属于类型1时,对函数进行排序意味着什么?
What does it means to sequence function, when those function are of type 1 ?
也就是说,您采用一个函数,然后应用一个返回相同类型函数的函数.我确实认为Reader是一种特定的技术,而与函数是函数1无关.与环境低谷相关的线程只是一个选择,如果需要,可以使用FunctionN来完成.
That is, you take a function and you apply a function that return the same type of function. I do think Reader is a specific technics, independent of the fact that the function are function 1. Threadying the environement trough is just a choice, and it could be done with FunctionN if we wanted to.
只是一种直觉.
EDIT1
此练习是如何在scala的FP编程中进行的:
Here is how the exercise comes in FP Programing in scala:
Hard: To cement your understanding of monads,
give a monad instance for the following type,
and explain what it means.
What are its primitive operations?
What is the action of flatMap? ......
case class Reader[R, A](run: R => A)
object Reader {
def readerMonad[R] = new Monad[({type f[x] = Reader[R,x]})#f] {
def unit[A](a: => A): Reader[R,A]
def flatMap[A,B](st: Reader[R,A])(f: A => Reader[R,B]): Reader[R,B]
}
}"
部分答案使我不满意
// The action of Reader's `flatMap` is to pass the `r` argument along to both the
// outer Reader and also to the result of `f`, the inner Reader. Similar to how
// `State` passes along a state, except that in `Reader` the "state" is read-only.
我了解我可以阅读代码.我认为该解释不足以清楚地回答练习的问题.我一直在寻找比代码做些简单描述更笼统的东西.
I understand that as i can read code. I Think that explanation falls short to answer clearly the question of the exercise. I was looking for something a bit more general than a bare description of what the code does.
例如,这意味着固定类型R的含义.这对链接返回具有相同输入参数类型的效果函数的计算意味着什么?
For instance, what does that mean have the type R fixed. What does that means to chain computation that return as effect Function that take the same input parameter type ?
推荐答案
Reader [R,A]
是 R =>上的包装.一个
(因此可以在类内部定义方法,而不能将其定义为函数的扩展方法).函数 f
可以包装到阅读器 Reader(f)
中,阅读器 r
可以包装到函数 r.run中.
.因此, Reader [R,A]
↔ R =>A
是同构.
Reader[R, A]
is a wrapper over R => A
(so methods can be defined just inside the class, not as extension methods for functions). A function f
can be wrapped to a reader Reader(f)
, a reader r
can be unwrapped to a function r.run
. So Reader[R, A]
↔ R => A
is an isomorphism.
Reader [R,?]
具有类型为 Monad
的类的实例.
Reader[R, ?]
has an instance of type class Monad
.
R =>?
具有类型为 Monad
的类的实例.
R => ?
has an instance of type class Monad
.
这篇关于Function1和Reader Monad之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!