Created
January 27, 2012 03:11
-
-
Save giuniu/1686712 to your computer and use it in GitHub Desktop.
mapBetween関数とりあえずやっつけ版
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 = 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