Skip to content

Instantly share code, notes, and snippets.

@piyo7
Created November 26, 2014 09:14
Show Gist options
  • Save piyo7/93c253e6e017e2c5c1dd to your computer and use it in GitHub Desktop.
Save piyo7/93c253e6e017e2c5c1dd to your computer and use it in GitHub Desktop.
一時オブジェクトの生成コスト回避による高速化 ref: http://qiita.com/piyo7/items/c8e576c6b010256c66f9
scala> Seq(1, 2, 3).view.map(_ + 1).map(_ * 2)
res0: scala.collection.SeqView[Int,Seq[_]] = SeqViewMM(...)
scala> res0.force
res1: Seq[Int] = List(4, 6, 8)
override def map[B, That](f: A => B)(implicit bf: CanBuildFrom[This, B, That]): That = {
newMapped(f).asInstanceOf[That]
}
protected def newMapped[B](f: A => B): Transformed[B] = new { val mapping = f } with AbstractTransformed[B] with Mapped[B]
trait Mapped[B] extends Transformed[B] {
protected[this] val mapping: A => B
def foreach[U](f: B => U) {
for (x <- self)
f(mapping(x))
}
final override protected[this] def viewIdentifier = "M"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment