Iterable.toMap accepting both key and value mapper

37 views
Skip to first unread message

Artur Biesiadowski

unread,
Jul 2, 2016, 6:58:47 AM7/2/16
to Xtend Programming Language
Any chance of adding following code to Iterator/IterableExtensions in one of future versions of xtend?

    public static <T, K, V> Map<K, V> toMap(Iterable<? extends T> values, Function1<? super T, K> computeKeys, Function1<? super T, V> computeValues) {
       
return toMap(values.iterator(),computeKeys,computeValues);
   
}
   
   
public static <T, K, V> Map<K, V> toMap(Iterator<? extends T> values, Function1<? super T, K> computeKeys, Function1<? super T, V> computeValues) {
       
if (computeKeys == null)
           
throw new NullPointerException("computeKeys");
       
if (computeValues == null)
           
throw new NullPointerException("computeValues");
       
Map<K, V> result = Maps.newLinkedHashMap();
       
while(values.hasNext()) {
            T t
= values.next();
            result
.put(computeKeys.apply(t), computeValues.apply(t));
       
}
       
return result;
   
}

Regards,
Artur Biesiadowski

Sven Efftinge (sven@efftinge.de)

unread,
Jul 4, 2016, 2:03:59 AM7/4/16
to xtend...@googlegroups.com
Yes, that would be useful. 
Could you open a PR against https://github.com/eclipse/xtext-xtend ?
Thanks!

--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Artur Biesiadowski

unread,
Jul 10, 2016, 6:35:52 AM7/10/16
to Xtend Programming Language


On Monday, July 4, 2016 at 8:03:59 AM UTC+2, Sven Efftinge wrote:
Yes, that would be useful. 
Could you open a PR against https://github.com/eclipse/xtext-xtend ?
Thanks!


https://github.com/eclipse/xtext-lib/pull/2

Wasn't as easy as I expected, due to CLA, sign-off footers and different project (xtend-lib rather than xtext-xtend), but hopefully it works now.

Regards,
Artur Biesiadowski
Reply all
Reply to author
Forward
0 new messages