View source code
Display the source code in std/range/package.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.range.Cycle/cycle - multiple declarations

Function cycle

Repeats the given forward range ad infinitum. If the original range is infinite (fact that would make Cycle the identity application), Cycle detects that and aliases itself to the range type itself. That works for non-forward ranges too. If the original range has random access, Cycle offers random access and also offers a constructor taking an initial position index. Cycle works with static arrays in addition to ranges, mostly for performance reasons.

auto auto cycle(R) (
  R input
)
if (isInputRange!R);

Cycle!R cycle(R) (
  R input,
  size_t index = 0
)
if (isRandomAccessRange!R && !isInfinite!R);

Cycle!R cycle(R) (
  ref R input,
  size_t index = 0
) @system
if (isStaticArray!R);

Note

The input range must not be empty.

Tip

This is a great way to implement simple circular buffers.

Example

import std.algorithm.comparison : 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 ]));

Alias/Struct Cycle

Repeats the given forward range ad infinitum. If the original range is infinite (fact that would make Cycle the identity application), Cycle detects that and aliases itself to the range type itself. That works for non-forward ranges too. If the original range has random access, Cycle offers random access and also offers a constructor taking an initial position index. Cycle works with static arrays in addition to ranges, mostly for performance reasons.

struct Cycle(R)
  
if (isForwardRange!R && !isInfinite!R);
alias Cycle(R) = R;
struct Cycle(R)
  
if (isStaticArray!R);

Struct Cycle

Constructors

NameDescription
this Range primitives

Properties

NameTypeDescription
front[get, set] autoRange primitives
save[get] CycleRange primitives

Methods

NameDescription
opIndex Range primitives
opIndexAssign Range primitives
opSlice Range primitives
popFront Range primitives

Alias Cycle

Struct Cycle

Constructors

NameDescription
this Range primitives

Properties

NameTypeDescription
front[get] inout(ElementType)Range primitives
save[get] inout(Cycle)Range primitives

Methods

NameDescription
opIndex Range primitives
opSlice Range primitives
popFront Range primitives

Note

The input range must not be empty.

Tip

This is a great way to implement simple circular buffers.

Example

import std.algorithm.comparison : 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, Jonathan M Davis, and Jack Stouffer. Credit for some of the ideas in building this module goes to Leonardo Maffi.

License

Boost License 1.0.