I am working with this code:
case class State[S, +A](run: S => (A, S)) { ... def flatMap[B](f: A => State[S, B]): State[S, B] = State(s => { val (a, s1) = run(s) f(a).run(s1) }) ... }
This is an abstraction for working with a purely functional state, from § 6 FP in Scala . run is a function parameter that takes state and emits a tuple of value and new state.
My question is around the s => syntax in this section:
... B] = State(s => { ...
It seems that the State 'constructor (i.e. apply ) is used to create a new State object. But what does s mean? Is this an “anonymous” state representing any state authority? If so, how is it different from this ? Or s corresponds to the input parameter run , i.e. s from:
... (run: S => ....
And why should I use a constructor to define a function? Note that the last character in the flatMap definition is ) not a } , which closes the apply State constructor.
This scenario is slightly different from the standard.
case class Person(name: String)
so I thought I would ask ...
source share