Skip to content

range

Create a range used for iterating

170 bytes

Usage

Given a start, end, value, and step size returns a generator that will yield values from start to end by step size. Useful for replacing for (let i = 0) with for of. Range will return a generator that for of will call one at a time, so it’s safe to create large ranges.

The interface is identical to list.

A hat tip to Python’s range functionality

import * as _ from 'radashi'
_.range(3) // yields 0, 1, 2, 3
_.range(0, 3) // yields 0, 1, 2, 3
_.range(0, 3, 'y') // yields y, y, y, y
_.range(0, 3, () => 'y') // yields y, y, y, y
_.range(0, 3, i => i) // yields 0, 1, 2, 3
_.range(0, 3, i => `y${i}`) // yields y0, y1, y2, y3
_.range(0, 3, obj) // yields obj, obj, obj, obj
_.range(0, 6, i => i, 2) // yields 0, 2, 4, 6
for (const i of _.range(0, 200, 10)) {
console.log(i) // => 0, 10, 20, 30 ... 190, 200
}
for (const i of _.range(0, 5)) {
console.log(i) // => 0, 1, 2, 3, 4, 5
}

Signatures

The range function can do a lot with different arguments.

range(size)

When givin a single argument, it’s treated as the size. Returns a generator that yields values from 0 to size.

_.range(3) // yields 0, 1, 2, 3

range(start, end)

When given two arguments, they’re treated as the start and end. Returns a generator that yields values from start to end

_.range(2, 6) // yields 2, 3, 4, 5, 6

range(start, end, value)

When given a third argument it’s treated as the value to be yielded in the generator. If the value is a function it will be called, with an index argument, to create every value.

_.range(2, 4, {}) // yields {}, {}, {}
_.range(2, 4, null) // yields null, null, null
_.range(2, 4, i => i) // yields 2, 3, 4

range(start, end, value, step)

When given a fourth argument it’s treated as the step size to skip when yielding values from start to end.

_.range(2, 4, i => i, 2) // yields 2, 4
_.range(25, 100, i => i, 25) // yields 25, 50, 75, 100