The OpenD Programming Language

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

import std.algorithm.sorting : sort;
import std.typecons : tuple, Tuple;

auto aa = ["a": 1, "b": 2, "c": 3];
Tuple!(string, int)[] pairs;

// Iteration over key/value pairs.
foreach (pair; aa.byPair)
{
    if (pair.key == "b")
        pairs ~= tuple("B", pair.value);
    else
        pairs ~= pair;
}

// Iteration order is implementation-dependent, so we should sort it to get
// a fixed order.
pairs.sort();
assert(pairs == [
    tuple("B", 2),
    tuple("a", 1),
    tuple("c", 3)
]);

Meta