简体   繁体   中英

Understanding combinators

I have a question about combinators from book FPiS on the 84.

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 definitions are type aliases. This means you can replace Rand[A] wherever you see it by RNG => (A, RNG) . So for instance, 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). The _ is a placeholder that does exactly the same thing, without having to name the parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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