A.H.
A.H.

Reputation: 66263

Combining two Iterables using standard library -- or -- map with multiple Iterables

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?

Upvotes: 4

Views: 1980

Answers (1)

Andrejs
Andrejs

Reputation: 27697

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 ]

Upvotes: 7

Related Questions