Discord is now Scala’s main chat platform. Please join us at https://discord.com/invite/scala
def map[A, B]: State[S, A] => (A => B) => State[S, B]
State[S, B]
State[S, B]
? at this point the only thing we have that type checks, it's the constructor
how do we create a
State[S, B]
? at this point the only thing we have that type checks, it's the constructor
so that's where we got to
S => (S, B)
def map[A, B](fa: State[S, A])(f: A => B) : State[S, B] = State[S, B] { s: S =>
// more work to do here
def result: (S, B) = ???
result
}
val a = get[Int].map(_ + 1).map(_.toString)
a.run(3)._1 // "4"
a.run(7)._1 // "8"
State
in its entirety yet (pure
, flatMap
, put
, modify
etc)
run
function"
get
, which build simple functions directly
map
, which build more complex functions based on previous State
computations
a
), end up having a more complex function which represents the sequence of transformations
run
functions, and get the result out
State.map(get[Int])(_ + 1).run(1)
in our case, because we do not use infix
pure
, flatMap
and put
modify
is get.flatMap(s => put(f(s))