Last active
December 14, 2015 01:49
-
-
Save hamnis/5008899 to your computer and use it in GitHub Desktop.
Unzip4 List
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 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