如题, 下面这个map2的方法声明理解存在问题,不理解rng这个地方的值到底是怎么传入的,如果这是scala的lambda表达式,那么rng的值是如何来的呢
附上 Rand 的定义
type Rand[+A] = RNG => (A, RNG)
def map2[A,B,C](ra: Rand[A], rb: Rand[B])(f: (A, B) => C): Rand[C] =
rng => {
val (a, r1) = ra(rng)
val (b, r2) = rb(r1)
(f(a, b), r2)
}
def both[A,B](ra: Rand[A], rb: Rand[B]): Rand[(A,B)] =
map2(ra, rb)((_, _))
map2的返回值是个函数,rng只是这个函数的参数。
在贴出的代码中没有对返回函数的调用,所以rng自然也没有值。
rng在函数体中被ra调用
ra的类型是Rand[A],也就是一个函数,接收RNG,返回(A,RNG).因而rng的类型是RNG并且代码中可以省略。