groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Schalk W. Cronjé <ysb...@gmail.com>
Subject RE: Shortest way of creating combinations of array elements
Date Thu, 12 Nov 2015 16:21:27 GMT
Yes!

<div>-------- Original message --------</div><div>From: "Winnebeck, Jason"
<Jason.Winnebeck@windstream.com> </div><div>Date:12/11/2015  16:16  (GMT+00:00)
</div><div>To: users@groovy.incubator.apache.org </div><div>Subject:
RE: Shortest way of creating combinations of array elements </div><div>
</div>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.
Mime
View raw message