Skip to content

Instantly share code, notes, and snippets.

@InvisibleTech
Last active September 27, 2015 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InvisibleTech/856dbba5b86925725d11 to your computer and use it in GitHub Desktop.
Save InvisibleTech/856dbba5b86925725d11 to your computer and use it in GitHub Desktop.
My implementation of a post on the Daily Scala
import scala.collection._
import scala.collection.generic._
//
// Simple example of custom Traversable that is provided by the Daily Scala:
// http://daily-scala.blogspot.com/2010/04/creating-custom-traversable.html
//
class MooCow[A](seq: A*) extends Traversable[A] with GenericTraversableTemplate[A, MooCow] with TraversableLike[A, MooCow[A]]{
override def companion = MooCow
def foreach[U](f: A => U) = seq.reverse.foreach(f)
}
// These are minimal implementations as indicated by the sources.
object MooCow extends TraversableFactory[MooCow] {
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, MooCow[A]] = new GenericCanBuildFrom[A]
def newBuilder[A] = new scala.collection.mutable.LazyBuilder[A, MooCow[A]] {
def result = {
val data = parts.foldLeft(List[A]()){(l,n) => l ++ n}
new MooCow(data:_*)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment