Implementation of flatMap() for State transition
问题 Exercise 6.8, Chiusano and Bjarnason, Functional Programming in Scala , p. 87 asks how one might implement flatMap() for the following trait: trait RNG { def nextInt: (Int, RNG) } type Rand[+A] = RNG => (A, RNG) The answer key gives the following solution: def flatMap[A,B](f: Rand[A])(g: A => Rand[B]): Rand[B] = rng => { val (a, r1) = f(rng) g(a)(r1) // We pass the new state along } Stackoverflow provides many answers to flatMap()/monad questions, but none which for me answered my questions