mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 22:50:38 +03:00
Added practical example to std.algorithm.iteration.group
This commit is contained in:
parent
c32240a5fe
commit
e656eec40f
1 changed files with 18 additions and 0 deletions
|
@ -1492,6 +1492,24 @@ if (isInputRange!R)
|
|||
tuple(4, 3u), tuple(5, 1u) ][]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Using group, an associative array can be easily generated with the count of each
|
||||
* unique element in the range.
|
||||
*/
|
||||
@safe unittest
|
||||
{
|
||||
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;
|
||||
|
||||
assert(result == ["a": 2U, "b": 2U, "c": 3U, "d": 1U, "e": 1U]);
|
||||
}
|
||||
|
||||
@safe unittest
|
||||
{
|
||||
import std.algorithm.comparison : equal;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue