Stateモナドの使い道 [フィボナッチ]

case class S(current: Int, next: Int) {
  def process() = S(next, current + next)
}

val fib: Stream[State[S, Int]] = Stream.continually(State(s => (s.process(), s.current)))
val state: State[S, Stream[Int]] = fib.take(15).sequence
println(state.run(S(0, 1)).value._2.toList) // List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377)