Scala and State Monad

I tried to understand the state Monad. Not as much as it is used, although it is not always easy to find. But every discussion that I find in the state Monad has basically the same information, and there is always something that I don’t understand.

Take this article for example. In it, the author has the following:

case class State[S, A](run: S => (A, S)) {
...
  def flatMap[B](f: A => State[S, B]): State[S, B] =
    State(s => {
      val (a, t) = run(s)
      f(a) run t
    })
...
}

I see that types are being built correctly. However, I do not understand the second runat all.

Perhaps I am misunderstanding the purpose of this monad. I had the impression from the HaskellWiki that the state monad was like a state machine with runtransitions (although in this case the state machine really does not have fixed state transitions, such as most state machines). If so, then (a, t)one transition will be presented in the code above . The application fwill be a modification of this value and State(generation of a new State object). This leaves me completely confused as to what the second is run. It would seem that this is the second "transition". But that makes no sense to me.

, run State (A, S), , , , . , .

, ? ?

: 12/22/2015

, , . .

map:

def map[B](f: A => B): State[S, B] =
  State(s => {
    val (a, t) = run(s)
    (f(a), t)
  })

, run.

, , , run , . , -, map. flatMap run. , "" .

@Filppo, run (1, List(2,3,4,5)), (2, List(3,4,5)), . map, (Map(a->2, b->3), List(4,5)).

-, , . . ?

2nd Edit: 12/22/2015

, REPL. , .

scala> val v = State(head[Int]).flatMap { a => State(head[Int]) }
v: State[List[Int],Int] = State(<function1>

scala> v.run(List(1,2,3,4,5))
res2: (Int, List[Int]) = (2,List(3, 4, 5))

, flatMap . @Filippo, , . ?

+4
3

" ", "".

def flatMap[B](f: A => State[S, B]): State[S, B] , f .

f, A. ? , run, A S, S.

- : s => val (a, t) = run(s) .... " S run, A S. " ".

A, f. , , f(a) State[S, B]. , , S Stats[S, B]:

(s: S) => 
   val (a, t) = run(s)
   f(a) //State[S, B]

S => State[S, B] - , ! State[S, B].

? State:

State(s => ... f(a))

, State S => (B, S), S => State[B, S]. (B, S) State[B, S].
, run , ! "" .

, , a flatMap:

s =>                   // when a state is provided
  val (a, t) = run(s)  // produce an `A` and a new state value
  val resState = f(a)  // produce a new `State[S, B]`
  resState.run(t)      // return `(S, B)`

S => (S, B), State.

" ":
- "" run
- f .

, "" . , : .

+3

state monad state state ( A):

type StatefulComputation[S, +A] = S => (A, S)

, "", run case class:

case class State[S, A](run: S => (A, S))

flatmap bind a state state 2 run s:

    // the `run` on the actual `state`
    val (a: A, nextState: S) = run(s)

    // the `run` on the bound `state`
    f(a).run(nextState)

EDIT flatmap 2 state

, .head List, A .tail S

// stateful computation: `S => (A, S)` where `S` is `List[A]`
def head[A](xs: List[A]): (A, List[A]) = (xs.head, xs.tail)

2 State(head[Int]):

// flatmap example
val result = for {
  a <- State(head[Int])
  b <- State(head[Int])
} yield Map('a' -> a,
            'b' -> b)

for-comprehension "" A, - b. S :

scala> result.run(List(1, 2, 3, 4, 5))
(Map(a -> 1, b -> 2),List(3, 4, 5))

? " " head[Int], run S:

s => run(s)

head (A) tail (b) . tail State(head[Int])

f(a).run(t)

f flatmap:

def flatMap[B](f: A => State[S, B]): State[S, B]

, , f , for-comprehension :

val result = State(head[Int]).flatMap {
  a => State(head[Int]).map {
    b => Map('a' -> a, 'b' -> b)
  }
}

f(a) A run(t) .

+2

@AlexyRaga . , @Filippo , , , . .

, , , , run. , . ( ). , , , . (this) . State (this) "" ( next ).

This is why the method flatMapis implemented as it is. When you create a new one State, you need a current pair of values ​​/ states from it based on the initial state that can be completed in the new object Stateas a function. You do not go into a new state. Just flip the generated state into a new object State.

I was too immersed in traditional state machines to see what was happening here.

Thanks again.

0
source

Source: https://habr.com/ru/post/1621183/


All Articles