View source code Display the source code in std/array.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

Function std.array.byPair

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

Prototype

auto byPair(Key, Value)(
  Value[Key] 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.

Example

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

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

// Iteration over key/value pairs.
foreach (pair; aa.byPair)
{
    pairs ~= pair;
}

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

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0.

Comments