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.

Note: The input range must not be empty.

Tip: This is a great way to implement simple circular buffers.

  1. struct Cycle(R)
    struct Cycle (
    R
    ) if (
    isForwardRange!R &&
    !isInfinite!R
    ) {}
  2. template Cycle(R)
  3. struct Cycle(R)

Constructors

this
this(R input, size_t index)
this(R input)
opIndex
auto ref opIndex(size_t n)
opIndexAssign
void opIndexAssign(ElementType!R val, size_t n)
opSlice
auto opSlice(size_t i, size_t j)
auto opSlice(size_t i, DollarToken )
popFront
void popFront()
opDollar
enum opDollar;
front
auto ref front [@property getter]
ElementType!R front [@property setter]
save
Cycle save [@property getter]
empty
enum bool empty;

Range primitives

Members

Functions

Manifest constants

Properties

Variables

Examples

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 ]));

Meta