Range - laforge49/Asynchronous-Functional-Programming GitHub Wiki

A sequence over a range of integers is a handy thing to have, especially when testing, and this is what the Range actor provides. The implementation of Range is straight forward and, like EmptySeq, usually operates synchronously.

class Range(start: Int, limit: Int)
  extends Sequence[Int, Int](mailbox, factory) {

  override def first(msg: AnyRef, rf: Any => Unit) {
    rf(KVPair(start, start))
  }

  override def current(msg: AnyRef, rf: Any => Unit) {
    var key = msg.asInstanceOf[Current[Int]].key
    if (key < start) key = start
    if (key >= limit) rf(null)
    else rf(KVPair(key, key))
  }

  override def next(msg: AnyRef, rf: Any => Unit) {
    var key = msg.asInstanceOf[Next[Int]].key
    if (key < start) key = start
    else key = key + 1
    if (key >= limit) rf(null)
    else rf(KVPair(key, key))
  }
}

The range test is pretty simple.

val range = new Range(4, 6)
println(Future(range, First()))
println(Future(range, Current(3)))
println(Future(range, Next(4)))
println(Future(range, Next(5)))

Output.

KVPair(4,4)
KVPair(4,4)
KVPair(5,5)
null

RangeTest

Tutorial