When given two Iterables
val keys = newLinkedList('foo', 'bar', 'bla')
val vals = newLinkedList(42, 43, 44)
I want to correlate each item in both lists like this:
val Iterable<Pair<String, Integer>> expected
= newLinkedList('foo'->42, 'bar'->43, 'bla'->44)
OK, I could do it by hand iterating over both lists.
On the other hand this smells like something where
For examples in Python this would be a no-brainer, because their map function can take multiple lists.
How can this be solved using Xtend2+ with miniumum code?
"Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code. These are by far the most easily (and most commonly) abused parts of Guava, and when you go to preposterous lengths to make your code "a one-liner," the Guava team weeps. " ( http://code.google.com/p/guava-libraries/wiki/FunctionalExplained - Etienne Neveu 2012-04-06 08:18
Pair
embedded into the language. This is what the x->y
operator does. So I think guava's philosophy does not apply in this case - A.H. 2012-04-06 09:09
val paired = zip(keys, vals)[k, v | k -> v]
would be more readable than doing it by hand. What I want to know: IS there something like this zip
thing on board somewhere - A.H. 2012-04-06 09:15
final Iterator it = vals.iterator();
expected = Iterables.transform(keys, new Function<String, Pair>() {
public Pair apply(String key) {
return new Pair(key, it.next());
}
});
Added by A.H.:
In Xtend this looks like this:
val valsIter = vals.iterator()
val paired = keys.map[ k | k -> valsIter.next ]