Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Last active December 10, 2018 13:06
Show Gist options
  • Save mpilquist/9ad05abee09b885f8ded to your computer and use it in GitHub Desktop.
Save mpilquist/9ad05abee09b885f8ded to your computer and use it in GitHub Desktop.
Minimal integration of State and Actor
import akka.actor._
import scalaz.State
/**
* Minimal integration between State monad and Akka's Actor.
*
* Usage: upon receiving a message, create a State instance and then call runState(st). The state value
* is persisted in an actor field between invocations of runState.
*/
abstract class StateActor[S] extends Actor {
protected def initialState: S
private var lastState: S = initialState
protected def runState[A](st: State[S, A]): A = {
val (newState, result) = st.run(lastState)
lastState = newState
result
}
}
@shumn
Copy link

shumn commented Apr 7, 2017

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment