Created
February 27, 2012 11:54
-
-
Save giuniu/1923244 to your computer and use it in GitHub Desktop.
mapBetween関数 trait版
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
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