Created
April 5, 2014 13:45
-
-
Save wookietreiber/9992177 to your computer and use it in GitHub Desktop.
Scala inverted zip extension method
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
import language.higherKinds | |
import collection.GenIterable | |
import collection.generic.CanBuildFrom | |
implicit class ZipInvert[A,CC[X] <: GenIterable[X]](coll: CC[A]) { | |
def zipInvert[A1 >: A, B, That](that: GenIterable[B])(implicit bf: CanBuildFrom[CC[A], (B, A1), That]): That = { | |
val b = bf(coll) | |
val these = coll.iterator | |
val those = that.iterator | |
while (these.hasNext && those.hasNext) | |
b += ((those.next, these.next)) | |
b.result | |
} | |
} | |
val numberList = List.range(1,5) | |
numberList zipInvert Stream.continually("a") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment