View source code
Display the source code in std/array.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.array.byPair

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

auto auto byPair(AA) (
  AA aa
)
if (isAssociativeArray!AA);

Parameters

NameDescription
aa The associative array to iterate over.

Returns

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).

Example

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)
]);

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0.