Groovy中有Scala'zip'函数吗? [英] Is there any analog for Scala 'zip' function in Groovy?

查看:78
本文介绍了Groovy中有Scala'zip'函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala中,我可以写下如下内容:

$ $ p $ $ $ $ $ $ $ a $ List $($ 1 $ b val b = List(4,5)
println(a zip b)

会产生 List((1,4),(2,5))作为输出。

现在我在Groovy中有两个集合,以类似的方式压缩它们。 Groovy的相当于Scala的 zip c>是 列表#转换 ,可以在清单列表中调用:

  assert [[转置()== [[1,4],[2,5]] 


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?

解决方案

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]]

这篇关于Groovy中有Scala'zip'函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆