Skip to content

Instantly share code, notes, and snippets.

@giuniu
Created February 27, 2012 11:54
Show Gist options
  • Save giuniu/1923244 to your computer and use it in GitHub Desktop.
Save giuniu/1923244 to your computer and use it in GitHub Desktop.
mapBetween関数 trait版
object mapBetween {
def main(args:Array[String]) = {
val range = new Range(1, 10, 1) with ExtSeq[Int]
println(range.mapBetween(_+_) mkString ",")
println(range.mapBetween(_*_) mkString ",")
println(range.mapBetween(_-_) mkString ",")
val range2 = 1 to 10
println(range2.mapBetween(_+_) mkString ",")
println(range2.mapBetween(_*_) mkString ",")
println(range2.mapBetween(_-_) mkString ",")
}
implicit def seqToExtSeq[A](seq: Seq[A]): ExtSeq[A] = {
import scala.collection.mutable.ArrayBuffer
new ArrayBuffer[A]() with ExtSeq[A] ++= seq
}
}
trait ExtSeq[+A] extends Seq[A] {
def mapBetween[B](f:(A,A)=>B): Iterator[B] = {
super.sliding(2).map(s=>f(s(0),s(1)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment