Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #350 Create an enumerated collection

Generate a collection of "enumerated" values.

For example, Python's `enumerate` built-in function.

https://docs.python.org/3/library/functions.html#enumerate

import java.util.List;
import java.util.stream.IntStream;
record E<T>(int i, T value) {}
<T> List<E<T>> enumerate(T ... a) {
    return IntStream.range(0, a.length)
                    .mapToObj(i -> new E<>(i, a[i]))
                    .toList();
}
a = 'abc xyz'.split()
for i, v in enumerate(a):
    print(i, v)
a = %w(abc xyz)
a.map.with_index{|v, i| [i, v] }

New implementation...
< >
reilas