Last active
September 27, 2015 00:36
-
-
Save InvisibleTech/856dbba5b86925725d11 to your computer and use it in GitHub Desktop.
My implementation of a post on the Daily Scala
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._ | |
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