View source code
Display the source code in std/algorithm/iteration.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.

std.algorithm.iteration.Group/group - multiple declarations

Function group

Groups consecutively equivalent elements into a single tuple of the element and the number of its repetitions.

Group!(pred,Range) group(alias pred, Range) (
  Range r
);

Similarly to uniq, group produces a range that iterates over unique consecutive elements of the given range. Each element of this range is a tuple of the element and the number of times it is repeated in the original range. Equivalence of elements is assessed by using the predicate pred, which defaults to "a == b". The predicate is passed to binaryFun, and can either accept a string, or any callable that can be executed via pred(element, element).

Parameters

NameDescription
pred Binary predicate for determining equivalence of two elements.
R The range type
r The input range to iterate over.

Returns

A range of elements of type Tuple!(ElementType!R, uint), representing each consecutively unique element and its respective number of occurrences in that run. This will be an input range if R is an input range, and a forward range in all other cases.

See Also

chunkBy, which chunks an input range into subranges of equivalent adjacent elements.

Example

import std.algorithm.comparison : equal;
import std.typecons : tuple, Tuple;

int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
assert(equal(group(arr), [ tuple(1, 1u), tuple(2, 4u), tuple(3, 1u),
    tuple(4, 3u), tuple(5, 1u) ][]));

Example

Using group, an associative array can be easily generated with the count of each unique element in the range.

import std.algorithm.sorting : sort;
import std.array : assocArray;

uint[string] result;
auto range = ["a", "b", "a", "c", "b", "c", "c", "d", "e"];
result = range.sort!((a, b) => a < b)
    .group
    .assocArray;

writeln(result); // ["a":2U, "b":2U, "c":3U, "d":1U, "e":1U]

Struct Group

Groups consecutively equivalent elements into a single tuple of the element and the number of its repetitions.

struct Group(alias pred, R)
  
if (isInputRange!R);

Similarly to uniq, group produces a range that iterates over unique consecutive elements of the given range. Each element of this range is a tuple of the element and the number of times it is repeated in the original range. Equivalence of elements is assessed by using the predicate pred, which defaults to "a == b". The predicate is passed to binaryFun, and can either accept a string, or any callable that can be executed via pred(element, element).

Constructors

NameDescription
this (input)

Properties

NameTypeDescription
front[get] auto
save[get] typeof(this)

Methods

NameDescription
popFront ()

Parameters

NameDescription
pred Binary predicate for determining equivalence of two elements.
R The range type
r The input range to iterate over.

Returns

A range of elements of type Tuple!(ElementType!R, uint), representing each consecutively unique element and its respective number of occurrences in that run. This will be an input range if R is an input range, and a forward range in all other cases.

See Also

chunkBy, which chunks an input range into subranges of equivalent adjacent elements.

Example

import std.algorithm.comparison : equal;
import std.typecons : tuple, Tuple;

int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
assert(equal(group(arr), [ tuple(1, 1u), tuple(2, 4u), tuple(3, 1u),
    tuple(4, 3u), tuple(5, 1u) ][]));

Example

Using group, an associative array can be easily generated with the count of each unique element in the range.

import std.algorithm.sorting : sort;
import std.array : assocArray;

uint[string] result;
auto range = ["a", "b", "a", "c", "b", "c", "c", "d", "e"];
result = range.sort!((a, b) => a < b)
    .group
    .assocArray;

writeln(result); // ["a":2U, "b":2U, "c":3U, "d":1U, "e":1U]

Authors

Andrei Alexandrescu

License

Boost License 1.0.