Skip to content

Instantly share code, notes, and snippets.

@jsuereth
Created March 8, 2012 21:09
Show Gist options
  • Select an option

  • Save jsuereth/2003466 to your computer and use it in GitHub Desktop.

Select an option

Save jsuereth/2003466 to your computer and use it in GitHub Desktop.
Lame-o Queue to show how to implement new collections
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