Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Created April 5, 2014 13:45
Show Gist options
  • Save wookietreiber/9992177 to your computer and use it in GitHub Desktop.
Save wookietreiber/9992177 to your computer and use it in GitHub Desktop.
Scala inverted zip extension method
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