You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
laforge49 edited this page Jul 31, 2011
·
6 revisions
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)))