Logo

Programming-Idioms

History of Idiom 7 > diff from v62 to v63

Edit summary for version 63 by programming-idioms.org:
[D] Array name is items

Version 62

2017-07-27, 23:58:21

Version 63

2017-07-30, 16:13:03

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;
Imports
import std.stdio, std.algorithm;
import std.range;
Code
x.enumerate.each!(a => writeln("i = ", a[0], " value = ", a[1]));
Code
items.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.
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) can't be indexed.
Doc URL
https://dlang.org/phobos/std_range.html#enumerate
Doc URL
https://dlang.org/phobos/std_range.html#enumerate
Demo URL
http://ideone.com/XSUx64
Demo URL
http://ideone.com/XSUx64