Skip to content

Instantly share code, notes, and snippets.

@hamnis
Last active December 14, 2015 01:49
Show Gist options
  • Save hamnis/5008899 to your computer and use it in GitHub Desktop.
Save hamnis/5008899 to your computer and use it in GitHub Desktop.
Unzip4 List
import java.net.URI
object Main extends App {
val list = List((1, URI.create("hello"), "hello", 23.2), (2, URI.create("bye"), "bye", 45.2))
val (ints, uris, strings, doubles) = list.unzip4
println(ints)
println(uris)
println(strings)
println(doubles)
implicit class ListUnzip4[+A](val list: List[A]) extends AnyVal {
def unzip4[A1, A2, A3, A4](implicit asTuple4: A => (A1, A2, A3, A4)): (List[A1], List[A2], List[A3], List[A4]) = {
val b1 = List.newBuilder[A1]
val b2 = List.newBuilder[A2]
val b3 = List.newBuilder[A3]
val b4 = List.newBuilder[A4]
for (abcd <- list) {
val (a, b, c, d) = asTuple4(abcd)
b1 += a
b2 += b
b3 += c
b4 += d
}
(b1.result(), b2.result(), b3.result(), b4.result())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment