Created
November 6, 2011 15:28
-
-
Save notyy/1343027 to your computer and use it in GitHub Desktop.
stacked modification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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