gpt4 book ai didi

scala - Scala 中这个表达式中 '_.nextInt' 的值是多少

转载 作者:行者123 更新时间:2023-12-01 08:21:44 25 4
gpt4 key购买 nike

我正在阅读《Scala 中的函数式编程》一书,并且遇到了以下代码:

trait RNG {
def nextInt: (Int, RNG)
}

type Rand[+A] = RNG => (A, RNG)

object Rand {
val int: Rand[Int] = _.nextInt

def unit[A](a: A): Rand[A] = rng => (a, rng)

// other code...
}

这行我看不懂

val int: Rand[Int] = _.nextInt

这意味着 int 是 Rand[Int] 类型的值,设置为 "_.nextInt" 但这个值是什么?_ 是通配符,我无法理解这个表达式的值。

P/S:谢谢你的回答

_.nextInt is (x:A)=>x.nextInt

最佳答案

_.nextIntx => x.nextInt 即 lambda。

关于scala - Scala 中这个表达式中 '_.nextInt' 的值是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56415067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com