Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Forked from giuniu/mapBetween.scala
Created February 27, 2012 14:05
Show Gist options
  • Save gakuzzzz/1924011 to your computer and use it in GitHub Desktop.
Save gakuzzzz/1924011 to your computer and use it in GitHub Desktop.
mapBetween関数 trait版
trait ExSeq[+A] {
self: Seq[A] =>
def mapBetween[B](f:(A,A)=>B): Iterator[B] = {
sliding(2).map(s=>f(s(0),s(1)))
}
}
object ExSeq {
import scala.collection.immutable.Range
def apply(r: Range): ExSeq[Int] = new Range(r.start, r.end, r.step) with ExSeq[Int]
}
object mapBetween {
def main(args: Array[String]) {
val range2 = 1 to 10
println(range2.mapBetween(_ + _) mkString ",")
println(range2.mapBetween(_ * _) mkString ",")
println(range2.mapBetween(_ - _) mkString ",")
}
implicit def seqToExtSeq[A](seq: Seq[A]) = new {
def mapBetween[B](f: (A, A) => B): Iterator[B] = {
seq.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