Logo

Programming-Idioms

History of Idiom 7 > diff from v61 to v62

Edit summary for version 62 by Bzzzzzzz:
New D implementation by user [Bzzzzzzz]

Version 61

2017-06-07, 15:45:20

Version 62

2017-07-27, 23:58:21

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Imports
import std.stdio, std.algorithm;
import std.range;
Code
x.enumerate.each!(a => writeln("i = ", a[0], " value = ", a[1]));
Comments bubble
enumerate can be used instead of foreach with an index when the statement is simple enough to be a lambda or when the foreach aggregate (the array like thing) cant be indexed.
Doc URL
https://dlang.org/phobos/std_range.html#enumerate
Demo URL
http://ideone.com/XSUx64