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.
							
						Module std.algorithm.iteration
This is a submodule of std.
It contains generic iteration algorithms.
| Function Name | Description | 
|---|---|
| cache | Eagerly evaluates and caches another range's front. | 
| cacheBidirectional | As above, but also provides backandpopBack. | 
| chunkBy | chunkBy!((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]. | 
| cumulativeFold | cumulativeFold!((a, b) => a + b)([1, 2, 3, 4])returns a
        lazily-evaluated range containing the successive reduced values1,3,6,10. | 
| each | each!writeln([1, 2, 3])eagerly prints the numbers1,2and3on their own lines. | 
| filter | filter!(a => a > 0)([1, -1, 2, 0, -3])iterates over elements1and2. | 
| filterBidirectional | Similar to filter, but also providesbackandpopBackat
        a small increase in cost. | 
| fold | fold!((a, b) => a + b)([1, 2, 3, 4])returns10. | 
| group | group([5, 2, 2, 3, 3])returns a range containing the tuplestuple(5, 1),tuple(2, 2), andtuple(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!(a => a * 2)([1, 2, 3])lazily returns a range with the numbers2,4,6. | 
| mean | Colloquially known as the average, mean([1, 2, 3])returns2. | 
| permutations | Lazily computes all permutations using Heap's algorithm. | 
| reduce | reduce!((a, b) => a + b)([1, 2, 3, 4])returns10.
        This is the old implementation offold. | 
| splitWhen | Lazily splits a range by comparing adjacent elements. | 
| splitter | Lazily splits a range by a separator. | 
| substitute | [1, 2]returns[0.1, 2]. | 
| sum | Same as fold, but specialized for accurate summation. | 
| uniq | Iterates over the unique elements in a range, which is assumed sorted. | 
Functions
| Name | Description | 
|---|---|
| 
									cache(range)
								 | cacheeagerly evaluates front ofrangeon 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(range)
								 | cacheeagerly evaluates front ofrangeon 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(r)
								 | Chunks an input range into subranges of equivalent adjacent elements.
 In other languages this is often called partitionBy,groupByorsliceWhen. | 
| 
									group(r)
								 | Groups consecutively equivalent elements into a single tuple of the element and the number of its repetitions. | 
| 
									joiner(r, sep)
								 | Lazily joins a range of ranges with a separator. The separator itself
is a range. If a separator is not provided, then the ranges are
joined directly without anything in between them (often called flattenin other languages). | 
| 
									mean(r)
								 | Finds the mean (colloquially known as the average) of a range. | 
| 
									permutations(r)
								 | Lazily computes all permutations of rusing Heap's algorithm. | 
| 
									splitter(r, s)
								 | Lazily splits a range using an element or range as a separator. Separator ranges can be any narrow string type or sliceable range type. | 
| 
									splitter(s)
								 | Lazily splits the character-based range sinto words, using whitespace as the
delimiter. | 
| 
									splitWhen(r)
								 | Splits a forward range into subranges in places determined by a binary predicate. | 
| 
									substitute(r, substs)
								 | Returns a range with all occurrences of substsinr.
replaced with their substitution. | 
| 
									sum(r)
								 | Sums elements of r, which must be a finite
input range. Although
conceptuallysum(r)is equivalent tofold!((a, b) => a +
b)(r, 0),sumuses specialized algorithms to maximize accuracy,
as follows. | 
| 
									uniq(r)
								 | 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". The predicate is passed tobinaryFun, and can either accept a string, or any callable
that can be executed viapred(element, element). If the given range is
bidirectional,uniqalso yields a
bidirectional range. | 
Structs
| Name | Description | 
|---|---|
| 
									Group
								 | Groups consecutively equivalent elements into a single tuple of the element and the number of its repetitions. | 
| 
									Permutations
								 | Lazily computes all permutations of rusing Heap's algorithm. | 
Templates
| Name | Description | 
|---|---|
| 
									cumulativeFold
								 | Similar to fold, but returns a range containing the successive reduced values.
The callcumulativeFold!(fun)(range, seed)first assignsseedto an
internal variableresult, also called the accumulator.
The returned range contains the valuesresult = fun(result, x)lazily
evaluated for each elementxinrange. Finally, the last element has the
same value asfold!(fun)(seed, range).
The one-argument versioncumulativeFold!(fun)(range)works similarly, but
it returns the first element unchanged and uses it as seed for the next
elements.
This function is also known as
    partial_sum,
    accumulate,
    scan,
    Cumulative Sum. | 
| 
									each
								 | Eagerly iterates over rand callsfunwith each element. | 
| 
									filter
								 | Implements the higher order filter function. The predicate is passed to unaryFun, and can either accept a string, or any callable
that can be executed viapred(element). | 
| 
									filterBidirectional
								 | Similar to filter, except it defines a
 bidirectional range.
 There is a speed disadvantage - the constructor spends time
 finding the last element in the range that satisfies the filtering
 condition (in addition to finding the first one). The advantage is
 that the filtered range can be spanned from both directions. Also,stdcan be applied against the filtered range. | 
| 
									fold
								 | Implements the homonym function (also known as accumulate,compress,inject, orfoldl) present in various programming
languages of functional flavor. The callfold!(fun)(range, seed)first assignsseedto an internal variableresult,
also called the accumulator. Then, for each elementxinrange,result = fun(result, x)gets evaluated. Finally,resultis returned. The one-argument versionfold!(fun)(range)works similarly, but it uses the first element of the range as the
seed (the range must be non-empty). | 
| 
									map
								 | Implements the homonym function (also known as transform) present
in many languages of functional flavor. The callmap!(fun)(range)returns a range of which elements are obtained by applyingfun(a)left to right for all elementsainrange. The original ranges are
not changed. Evaluation is done lazily. | 
| 
									reduce
								 | Implements the homonym function (also known as accumulate,compress,inject, orfoldl) present in various programming
languages of functional flavor. There is alsofoldwhich does
the same thing but with the opposite parameter order.
The callreduce!(fun)(seed, range)first assignsseedto
an internal variableresult, also called the accumulator.
Then, for each elementxinrange,result = fun(result, x)gets evaluated. Finally,resultis returned.
The one-argument versionreduce!(fun)(range)works similarly, but it uses the first element of the range as the
seed (the range must be non-empty). | 
| 
									substitute
								 | Returns a range with all occurrences of substsinr.
replaced with their substitution. | 
Authors
License
					Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.