Skip to content

Instantly share code, notes, and snippets.

Created Aug 10, 2012
What would you like to do?
Get vs State
// In the following code, Get is a type which has previously been called State - a name which
// the author found to be confusing, since instances aren't actually states!
// Instances are in fact functions which obtain (get) a value that depends on some state.
// The following code demonstrates using a Get to set as well as get!
// claim: even this is less confusing than using the name State!
// N.B. Here, 'State' is the type of the er, state.
object app extends App {
type Get[A, S] = S => (A, S)
case class State(n: Int)
def set(n: Int): State = {
val get = new Get[Unit, State] {
def apply(s0: State) = ((), s0)
val (_, s) = get(State(n))
def get(s0: State): (Int, State) = {
val get = new Get[Int, State] {
def apply(s: State) = (s.n, s)
// test that get returns the value passed to set
val s0 = set(1)
val (n, s1) = get(s0)
assert(n == 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment