-
-
Save gakuzzzz/1924011 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
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] | |
} |
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 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