std.range.cycle
- multiple declarations
- Function cycle
- Struct Cycle
Function cycle
Repeats the given forward range ad infinitum. If the original range is
infinite (fact that would make
the identity application),
Cycle
detects that and aliases itself to the range type
itself. If the original range has random access, Cycle
offers
random access and also offers a constructor taking an initial position
Cycle
. index
works with static arrays in addition to ranges,
mostly for performance reasons.
Cycle
Prototypes
Cycle!R cycle(R)( R input ) if (isForwardRange!R && !isInfinite!R); Cycle!R cycle(R)( R input, size_t index = 0 ) if (isRandomAccessRange!R && !isInfinite!R);
Note
The input
range must not be empty.
Tip
This is a great way to implement simple circular buffers.
Example
import std.algorithm : equal; import std.range : cycle, take; // Here we create an infinitive cyclic sequence from [1, 2] // (i.e. get here [1, 2, 1, 2, 1, 2 and so on]) then // take 5 elements of this sequence (so we have [1, 2, 1, 2, 1]) // and compare them with the expected values for equality. assert(cycle([1, 2]).take(5).equal([ 1, 2, 1, 2, 1 ]));
Struct Cycle
Repeats the given forward range ad infinitum. If the original range is
infinite (fact that would make
the identity application),
Cycle
detects that and aliases itself to the range type
itself. If the original range has random access, Cycle
offers
random access and also offers a constructor taking an initial position
Cycle
index
.
works with static arrays in addition to ranges,
mostly for performance reasons.
Cycle
Note
The input range must not be empty.
Tip
This is a great way to implement simple circular buffers.
Example
import std.algorithm : equal; import std.range : cycle, take; // Here we create an infinitive cyclic sequence from [1, 2] // (i.e. get here [1, 2, 1, 2, 1, 2 and so on]) then // take 5 elements of this sequence (so we have [1, 2, 1, 2, 1]) // and compare them with the expected values for equality. assert(cycle([1, 2]).take(5).equal([ 1, 2, 1, 2, 1 ]));
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.