Last active
December 10, 2018 13:06
-
-
Save mpilquist/9ad05abee09b885f8ded to your computer and use it in GitHub Desktop.
Minimal integration of State and Actor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!