Skip to main content
Module

x/aitertools/mod.ts>Range

Well-tested utility functions dealing with async iterables
Go to Latest
class Range
implements Iterable<T>, AsyncIterable<T>
import { Range } from "https://deno.land/x/aitertools@0.5.0/mod.ts";

An immutable sequence of numbers. It implements both Iterable and AsyncIterable.

It is similar to Python's range() function.

Constructors

new
Range(
start: T,
stop: T,
step: T,
)

Constructs a new Range object.

Type Parameters

T extends number | bigint

The type of the elements in the range. It must be either number or bigint.

Properties

readonly
length: number

The length of the range. Note that it guarantees to return the same value as Array.from(range).length.

import { range } from "./range.ts";

console.log(range(10, -10, -3.5).length);

The above example will print 6.

readonly
start: T

The start of the range. It must be a finite number.

readonly
step: T

The step of the range. It must be a finite number, and cannot be zero.

readonly
stop: T

The stop of the range. It must be a finite number.

Methods

at(index: number): T | undefined

Returns the element at the specified index in the range. Note that it guarantees to return the same value as Array.from(range).at(index).

import { range } from "./range.ts";

const r = range(10, -10, -3.5);
console.log(r.at(3), r.at(-1));

The above example will print the following 2 lines:

-0.5
-7.5
toString(): string

Represents the range as a string.

[Symbol.asyncIterator](): AsyncIterator<T>

Iterates over the elements of the range, in an asynchronous manner.

import { range } from "./range.ts";

for await (const value of range(4)) console.log(value);

The above example will print the following 4 lines:

0
1
2
3
[Symbol.iterator](): Iterator<T>

Iterates over the elements of the range.

import { range } from "./range.ts";

for (const value of range(4n)) console.log(value);

The above example will print the following 4 lines:

0
1
2
3