View source code Display the source code in std/range.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

Function std.range.iota

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

Prototypes

auto iota(B, E, S)(
  B begin,
  E end,
  S step
)
if ((isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E))) && isIntegral!S);

auto iota(B, E)(
  B begin,
  E end
)
if (isFloatingPoint!(CommonType!(B, E)));

auto iota(B, E)(
  B begin,
  E end
)
if (isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)));

auto iota(E)(
  E end
);

auto iota(B, E, S)(
  B begin,
  E end,
  S step
)
if (isFloatingPoint!(CommonType!(B, E, S)));

auto iota(B, E)(
  B begin,
  E end
)
if (!isIntegral!(CommonType!(B, E)) && !isFloatingPoint!(CommonType!(B, E)) && !isPointer!(CommonType!(B, E)) && is(typeof((ref B b)
{
++b;
}
)) && (is(typeof(B.init < E.init)) || is(typeof(B.init == E.init))));

Parameters

NameDescription
begin The starting value.
end The value that serves as the stopping criterion. This value is not included in the range.
step The value to add to the current value at each iteration.

Returns

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.

Example

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 : map, copy;
    import std.format;
    iota(0, 5).map!(i => format("%s ", i)).copy(stdout.lockingTextWriter());
    writeln();
}

Example

import std.algorithm : equal;
import std.math : approxEqual;

auto r = iota(0, 10, 1);
assert(equal(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][]));
r = iota(0, 11, 3);
assert(equal(r, [0, 3, 6, 9][]));
assert(r[2] == 6);
auto rf = iota(0.0, 0.5, 0.1);
assert(approxEqual(rf, [0.0, 0.1, 0.2, 0.3, 0.4]));

Example

User-defined types such as std.bigint.BigInt are also supported, as long as they can be incremented with ++ and compared with < or ==.

import std.algorithm.comparison : equal;
import std.bigint;

auto s = BigInt(1_000_000_000_000);
auto e = BigInt(1_000_000_000_003);
auto r = iota(s, e);
assert(r.equal([
    BigInt(1_000_000_000_000),
    BigInt(1_000_000_000_001),
    BigInt(1_000_000_000_002)
]));

Authors

Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.

License

Boost License 1.0.

Comments