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

Module std.algorithm.iteration

This is a submodule of std.algorithm. It contains generic iteration algorithms.

Cheat Sheet
Function Name Description
cache Eagerly evaluates and caches another range's front.
cacheBidirectional As above, but also provides back and popBack.
chunkyBy chunkyBy!((a,b) => a[1] == b[1])([[1, 1], [1, 2], [2, 2], [2, 1]]) returns a range containing 3 subranges: the first with just [1, 1]; the second with the elements [1, 2] and [2, 2]; and the third with just [2, 1].
each each!writeln([1, 2, 3]) eagerly prints the numbers 1, 2 and 3 on their own lines.
filter filter!"a > 0"([1, -1, 2, 0, -3]) iterates over elements 1 and 2.
filterBidirectional Similar to filter, but also provides back and popBack at a small increase in cost.
group group([5, 2, 2, 3, 3]) returns a range containing the tuples tuple(5, 1), tuple(2, 2), and tuple(3, 2).
joiner joiner(["hello", "world!"], "; ") returns a range that iterates over the characters "hello; world!". No new string is created - the existing inputs are iterated.
map map!"2 * a"([1, 2, 3]) lazily returns a range with the numbers 2, 4, 6.
reduce reduce!"a + b"([1, 2, 3, 4]) returns 10.
splitter Lazily splits a range by a separator.
sum Same as reduce, but specialized for accurate summation.
uniq Iterates over the unique elements in a range, which is assumed sorted.

Functions

Name Description
cache cache eagerly evaluates front of range on each construction or call to popFront, to store the result in a cache. The result is then directly returned when front is called, rather than re-evaluated.
cacheBidirectional cache eagerly evaluates front of range on each construction or call to popFront, to store the result in a cache. The result is then directly returned when front is called, rather than re-evaluated.
chunkBy Chunks an input range into subranges of equivalent adjacent elements.
group Groups consecutively equivalent elements into a single tuple of the element and the number of its repetitions.
joiner Lazily joins a range of ranges with a separator. The separator itself is a range. If you do not provide a separator, then the ranges are joined directly without anything in between them.
splitter Lazily splits a range using an element as a separator. This can be used with any narrow string type or sliceable range type, but is most popular with string types.
splitter Similar to the previous overload of splitter, except this one uses another range as a separator. This can be used with any narrow string type or sliceable range type, but is most popular with string types.
splitter Similar to the previous overload of splitter, except this one does not use a separator. Instead, the predicate is an unary function on the input range's element type.
splitter Lazily splits the string s into words, using whitespace as the delimiter.
sum Sums elements of r, which must be a finite input range. Although conceptually sum(r) is equivalent to reduce!((a, b) => a + b)(0, r), sum uses specialized algorithms to maximize accuracy, as follows.
uniq Lazily iterates unique consecutive elements of the given range (functionality akin to the uniq system utility). Equivalence of elements is assessed by using the predicate pred, by default "a == b". If the given range is bidirectional, uniq also yields a bidirectional range.

Templates

Name Description
each Eagerly iterates over r and calls pred over each element.
filter auto filter(Range)(Range rs) if (isInputRange!(Unqual!Range));
filterBidirectional auto filterBidirectional(Range)(Range r) if (isBidirectionalRange!(Unqual!Range));
map auto map(Range)(Range r) if (isInputRange!(Unqual!Range));
reduce Implements the homonym function (also known as accumulate, compress, inject, or foldl) present in various programming languages of functional flavor. The call reduce!(fun)(seed, range) first assigns seed to an internal variable result, also called the accumulator. Then, for each element x in range, result = fun(result, x) gets evaluated. Finally, result is returned. The one-argument version reduce!(fun)(range) works similarly, but it uses the first element of the range as the seed (the range must be non-empty).

Authors

Andrei Alexandrescu

License

Boost License 1.0.

Comments