Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any analog for Scala 'zip' function in Groovy?

Tags:

scala

groovy

In Scala I can write something like this:

val a = List(1, 2, 3) val b = List(4, 5) println(a zip b) 

That would produce List((1,4), (2,5)) as output.
Now I have two collections in Groovy and want to zip them in similar fashion. What is the simplest way to do this?

like image 384
Sergey Weiss Avatar asked Dec 14 '12 13:12

Sergey Weiss


1 Answers

Groovy's equivalent of Scala's zip is List#transpose, which can be called on a list of lists:

assert [[1, 2, 3], [4, 5]].transpose() == [[1, 4], [2, 5]] 
like image 182
epidemian Avatar answered Sep 28 '22 14:09

epidemian