iota

Creates a range of values that span the given starting and stopping values.

Parameters

begin B

The starting value.

end E

The value that serves as the stopping criterion. This value is not included in the range.

step S

The value to add to the current value at each iteration.

Return Value

Type: auto

A range that goes through the numbers begin, begin + step, begin + 2 * step, ..., up to and excluding end.

The two-argument overloads have step = 1. If begin < end && step < 0 or begin > end && step > 0 or begin == end, then an empty range is returned. If step == 0 then begin == end is an error.

For built-in types, the range returned is a random access range. For user-defined types that support ++, the range is an input range.

An integral iota also supports in operator from the right. It takes the stepping into account, the integral won't be considered contained if it falls between two consecutive values of the range. contains does the same as in, but from lefthand side.

Examples

void main()
{
    import std.stdio;

    // The following groups all produce the same output of:
    // 0 1 2 3 4

    foreach (i; 0 .. 5)
        writef("%s ", i);
    writeln();

    import std.range : iota;
    foreach (i; iota(0, 5))
        writef("%s ", i);
    writeln();

    writefln("%(%s %|%)", iota(0, 5));

    import std.algorithm.iteration : map;
    import std.algorithm.mutation : copy;
    import std.format;
    iota(0, 5).map!(i => format("%s ", i)).copy(stdout.lockingTextWriter());
    writeln();
}
import std.algorithm.comparison : equal;
import std.math.operations : isClose;

auto r = iota(0, 10, 1);
assert(equal(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
assert(equal(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
assert(3 in r);
assert(r.contains(3)); //Same as above
assert(!(10 in r));
assert(!(-8 in r));
r = iota(0, 11, 3);
assert(equal(r, [0, 3, 6, 9]));
assert(r[2] == 6);
assert(!(2 in r));
auto rf = iota(0.0, 0.5, 0.1);
assert(isClose(rf, [0.0, 0.1, 0.2, 0.3, 0.4]));

Meta