Skip to content

Instantly share code, notes, and snippets.

@giuniu
Created January 27, 2012 03:11
Show Gist options
  • Save giuniu/1686712 to your computer and use it in GitHub Desktop.
Save giuniu/1686712 to your computer and use it in GitHub Desktop.
mapBetween関数とりあえずやっつけ版
object MapBetween {
def main(args:Array[String]) = {
val range = 1 to 10
println(mapBetween(range, (a1:Int,a2:Int)=>a1+a2))
println(mapBetween(range, (a1:Int,a2:Int)=>a1*a2))
println(mapBetween(range, (a1:Int,a2:Int)=>a1-a2))
}
def mapBetween[A,B,T>:A](seq:Seq[A], f:(T,T)=>B): Seq[B] = {
((Seq[B](), seq.head) /: seq.tail) {(tup:(Seq[B], A), a:A) => (tup._1 :+ f(tup._2, a), a)}._1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment