tenderowls / moorka

ABANDONED
21 stars 5 forks source link

FSM and Var's apply/update #32

Closed fomkin closed 9 years ago

fomkin commented 9 years ago

We decide to resurrect Var's apply/update methods and add new FSM class with withMod behavior. So we will have two stateful sources. FSM will not have special behavior for stateful bindings that Var.withMod currently has.

// Dirty, imperative `Var`
val c = Channel[Int]()
val x = Var(0)
c foreach { c => 
  if (x() == 0) x() = c 
}
c.pull(10)
c.pull(20)
assert(x() == 10)

// Pure, functional FSM
val c = Channel[Int]()
val x = FSM(0) {
  case 0  => c
  case _ => Dummy
}
c.pull(10)
c.pull(20)
x once { x =>
  assert(x == 10)
}