You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Schalk Cronjé <ys...@gmail.com> on 2015/11/12 17:06:26 UTC
Shortest way of creating combinations of array elements
I am trying to find the shortest method of creating pairs of values from
two lists.
def a = [ 1,2,3,4,5,6 ]
def b = [ 'a','b','c' ]
assert [ [ 1,'a'], [2,'a'], ... [5,'c'],[6,'c'] == f(a,b)
The question is what te shortest possible implementation f(a,b) looks like.
I know a.collect { item -> b.collect { [item,it] } } should work, but
wondering if there is something better.
--
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r
RE: Shortest way of creating combinations of array elements
Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
Is this what you want?
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Iterable.html#combinations%28%29
assert [['a', 'b'],[1, 2, 3]].combinations() == [['a', 1], ['b', 1], ['a', 2], ['b', 2], ['a', 3], ['b', 3]]
assert [[2, 3],[4, 5, 6]].combinations {x,y -> x*y } == [8, 12, 10, 15, 12, 18]
From: Schalk Cronjé [mailto:ysb33r@gmail.com]
Sent: Thursday, November 12, 2015 11:06 AM
To: users@groovy.incubator.apache.org
Subject: Shortest way of creating combinations of array elements
I am trying to find the shortest method of creating pairs of values from two lists.
def a = [ 1,2,3,4,5,6 ]
def b = [ 'a','b','c' ]
assert [ [ 1,'a'], [2,'a'], ... [5,'c'],[6,'c'] == f(a,b)
The question is what te shortest possible implementation f(a,b) looks like.
I know a.collect { item -> b.collect { [item,it] } } should work, but wondering if there is something better.
--
Schalk W. Cronjé
Twitter / Ello / Toeter : @ysb33r
----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.