Cartesian products


#1

In Groovy, given a sequence of function references (algorithms), and a sequence of pairs of integers (data), I can create the Cartesian product of these:

algorithms.collectMany{algorithm -> data.collect{datum -> [algorithm, datum[0], datum[1]]}}

it may be just the stress of having to get stuff together for a presentation on Saturday, but I cannot think of a good way of doing the same thing in Kotlin. Anyone any thoughts to help me achieve success?


#2

I don't know groovy very well, but do you mean something like this?

algorithms.flatMap { algorithm -> data.map { datum -> algorithm(datum[0], datum[1]) } }


#3

Yes the stress was a factor :-( flatMap was the thing I missed. However:

In the end I need an Object[][]. I can currently get a [Ljava.util.List; but that is not good enough.

array(algorithms.flatMap({algorithm -> data.map({datum -> array(algorithm, *datum)})}))


#4

Just curious: why do you need Object[][]?


#5

It is a requirement of TestNG that DataProviders deliver Object[][]. C├ędric has mentioned that he has no choice, it has to be this way.