简体   繁体   English

了解组合器

[英]Understanding combinators

I have a question about combinators from book FPiS on the 84. 我有一个关于84本书FPiS上的组合器的问题。

object RNG {

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

val int: Rand[Int] = _.nextInt

How do I interpret type Rand[+A] = RNG => (A, RNG) , and what does the underline by _.nextInt mean? 如何解释type Rand[+A] = RNG => (A, RNG) ,_。 _.nextInt的下划线是_.nextInt意思?

type definitions are type aliases. type定义是类型别名。 This means you can replace Rand[A] wherever you see it by RNG => (A, RNG) . 这意味着您可以将Rand[A]替换为RNG => (A, RNG) So for instance, Rand[Int] = RNG => (Int, RNG) . 因此,例如, Rand[Int] = RNG => (Int, RNG)

Now, since we know int is a function that takes an RNG , we can define it as an anonymous function, such as (r: RNG) => r.nextInt (you'll probably see the definition for nextInt in the RNG trait). 现在,由于我们知道int是采用RNG的函数,因此我们可以将其定义为匿名函数,例如(r: RNG) => r.nextInt (您可能会在RNG特征中看到nextInt的定义) 。 The _ is a placeholder that does exactly the same thing, without having to name the parameter. _是一个占位符,可以执行完全相同的操作,而不必命名参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM