Groovy中有Scala'zip'函数吗?

编程入门 行业动态 更新时间:2024-10-28 08:20:10
本文介绍了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'函数吗?

本文发布于:2023-10-23 04:17:44,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中有   函数   Groovy   Scala   zip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!