Created
March 8, 2012 21:09
-
-
Save jsuereth/2003466 to your computer and use it in GitHub Desktop.
Lame-o Queue to show how to implement new collections
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
| package misc | |
| import collection.SeqLike | |
| import scala.collection.mutable.Builder | |
| import scala.collection.mutable.ListBuffer | |
| import scala.collection.generic.SeqFactory | |
| import scala.collection.generic.GenericTraversableTemplate | |
| import scala.collection.generic.CanBuildFrom | |
| object Queue extends SeqFactory[Queue] { | |
| def newBuilder[A]: Builder[A, Queue[A]] = new ListBuffer[A] mapResult (new Queue(_)) | |
| implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, Queue[A]] = new GenericCanBuildFrom[A] | |
| } | |
| class Queue[+A] private (private val as: List[A]) extends Seq[A] with SeqLike[A, Queue[A]] with GenericTraversableTemplate[A, Queue] { | |
| override def companion = Queue | |
| def apply(i: Int): A = as(i) | |
| def length = as.length | |
| def iterator = as.iterator | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment