fn agent (val) -> receive {
  (:set, new) -> agent (new)
  (:get, pid) -> {
    send (pid, (:response, val))
    agent (val)
  }
  (:update, f) -> agent (f (val))
}

fn agent/set (pid, val) -> {
  send (pid, (:set, val))
  val
}

fn agent/get (pid) -> {
  send (pid, (:get, self ()))
  receive {
    (:response, val) -> val
  }
}

fn agent/update (pid, f) -> {
  send (pid, (:update, f))
  agent/get (pid)
}

let myagent = spawn! (fn () -> agent (42))

print! ("incrementing agent value to", agent/update (myagent, inc))

:done!
