fxRange
A lazy sequence of integers from start (inclusive) to end (exclusive), stepping by any amount.
Lecture
fxRange generates integers on demand, using a sync*
generator — nothing is allocated up front. Called with one argument,
fxRange(4) counts 0, 1, 2, 3 (i.e. 0
up to, but not including, that single argument — it is the end, though the
parameter is named start). Called with two,
fxRange(1, 4) counts 1, 2, 3. A third argument
sets the step — including a negative step to count downward,
e.g. fxRange(4, 1, -1) gives 4, 3, 2.
Because it's lazy, fxRange(1000000) is essentially free to
create — the million integers only get produced as something downstream
(usually .take(n)) actually pulls them. This makes
fxRange the go-to source for demonstrating laziness, and a
handy stand-in whenever you need "the first N things" without building
a real collection first.
Like FxTS's range, which is a finite generator over
numbers, fxRange is always finite — there's no
unbounded/infinite form here (that's cycle's job). If you
want an endless counter, pair fxRange with cycle
and take.
Demo 1 · Counting up, down, and by steps
Demo 2 · Laziness in a chain
fxRange(1000000) produces nothing up front — only the 4
elements take(4) asks for actually run through
map:
Try it yourself
Exercise: build the even numbers from 2 to 10 inclusive using
fxRange's step argument.