Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Cats State Monad

I have a class that has a one parameter method that produces a result and returns an object like itself but with updated state for subsequent use.

For example, below contains a simple example of such a class and how I might use it:

case class Foo(x: Double) {
  def bar(y: Double): (Foo, Double) = (Foo(x + y), x / (x + y))
}

val res = Vector(1.0,2.0,3.0,4.0).foldLeft((Foo(0), 0.0))((foo, x) => foo._1.bar(x))

res._1.bar(3.0)

I have looked at the Cats State monad and was hopeful that I could use it to avoid threading the state (the "x" member) around. The examples here are close to what I want but the function that returns the new state does not have any parameters and state is not passed around in a loop-like operation (instead it's passed between expressions). I am a complete novice when it comes to Cats but am I barking up the wrong tree?

like image 720
Richard Redding Avatar asked Dec 19 '18 13:12

Richard Redding


1 Answers

Below you can find the way how cats state monad can be adapated to your case. However I had some problems with sequencing List[State[S, A]] to have State[S, List[A]] in cats, so I have written a function sequence for that. If someone knew how to do that I would be interested :)

import cats.data._

case class Foo(x: Double)

def bar(y: Double): State[Foo, Double] = for {
  foo <- State.get[Foo]
  _ <- State.set(Foo(foo.x + y))

} yield foo.x / (foo.x + y)

val xs: List[State[Foo, Double]] = List(1.0, 2.0, 3.0, 4.0).map(bar)

def sequence(xs: List[State[Foo, Double]]): State[Foo, List[Double]] =
  xs.foldLeft(State.pure[Foo, List[Double]](List.empty[Double])) { (acc, x) =>
    for {
      xs <- acc
      xx <- x
    } yield xx :: xs
  }

val s = sequence(xs)
val ss = s.map(_.head)

s.run(Foo(0)).value
ss.run(Foo(0)).value

ss.flatMap(_ => bar(3)).run(Foo(0)).value

In result you get

res0: (Foo, List[Double]) = (Foo(10.0),List(0.6, 0.5, 0.3333333333333333, 0.0))
res1: (Foo, Double) = (Foo(10.0),0.6)

res2: (Foo, Double) = (Foo(13.0),0.7692307692307693)
like image 108
slavik Avatar answered Sep 30 '22 04:09

slavik