SethTisue on 2.12.x
2.12: new Scala SHA (compare)
Hello, I'm going through the Functional Programming in Scala and on the chapter about State we have:
case class State[S, +A](run: S => (A, S))
then we have a method
def simulateMachine(inputs: List[Input]): State[Machine, (Int, Int)]
val m: Machine = Machine(true, 5, 10) Candy.simulateMachine(inputs).run(m)
What is making me confused is that "m" is of type machine and "run" expects a "S => (A, S)" function... so how could I pass Machine to "run"?
.run, you access that field
(), you call
Function.apply(under the hood) therefore passing the
Sto the function
Stype parameter in this case is equal to
Machine, which is why you can pass a
Machineto the function
Candy.simulateMachine(inputs).run.apply(m) // we are calling the apply method on the run value which is an instance of the function class
S => (A, S)Function.
when you create an State you have to pass a Function.
I think this view is confusing
but where is the definition of run where it takes the S and
so you have various
State computations, right?