byPair

Construct a range iterating over an associative array by key/value tuples.

byPair
(
AA
)
(
AA aa
)

Parameters

aa AA

The associative array to iterate over.

Return Value

Type: auto

A forward range of Tuple's of key and value pairs from the given associative array. The members of each pair can be accessed by name (.key and .value). or by integer index (0 and 1 respectively).

Examples

1 import std.algorithm.sorting : sort;
2 import std.typecons : tuple, Tuple;
3 
4 auto aa = ["a": 1, "b": 2, "c": 3];
5 Tuple!(string, int)[] pairs;
6 
7 // Iteration over key/value pairs.
8 foreach (pair; aa.byPair)
9 {
10     if (pair.key == "b")
11         pairs ~= tuple("B", pair.value);
12     else
13         pairs ~= pair;
14 }
15 
16 // Iteration order is implementation-dependent, so we should sort it to get
17 // a fixed order.
18 pairs.sort();
19 assert(pairs == [
20     tuple("B", 2),
21     tuple("a", 1),
22     tuple("c", 3)
23 ]);

Meta

Suggestion Box / Bug Report