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?