Skip to content

Instantly share code, notes, and snippets.

@notyy
Created November 6, 2011 15:28
Show Gist options
  • Save notyy/1343027 to your computer and use it in GitHub Desktop.
Save notyy/1343027 to your computer and use it in GitHub Desktop.
stacked modification
import scala.collection.mutable.ArrayBuffer
abstract class IntQueue {
def get(): Int
def put(x:Int)
}
class BasicIntQueue extends IntQueue {
private val buf = new ArrayBuffer[Int]
def get() = buf.remove(0)
def put(x:Int) {buf += x }
}
trait Doubling extends IntQueue {
abstract override def put(x:Int) { super.put(2 * x) }
}
trait Incrementing extends IntQueue {
abstract override def put(x:Int) { super.put(x + 1) }
}
----------------------
run:
scala> val queue1 = new BasicIntQueue with Doubling with Incrementing
queue1: BasicIntQueue with Doubling with Incrementing = $anon$1@7595edfe
scala> val queue2 = new BasicIntQueue with Incrementing with Doubling
queue2: BasicIntQueue with Incrementing with Doubling = $anon$1@7fbcac12
scala> queue1.put(10)
scala> queue1.get
res12: Int = 22
scala> queue2.put(10)
scala> queue2.get
res14: Int = 21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment