I wonder if it would be a good idea to extend 'range' to be able to
express that concept.
Here's the doc for 'range':
clojure/range
([end] [start end] [start end step])
Returns a lazy seq of nums from start (inclusive) to end
(exclusive), by step, where start defaults to 0 and step to 1.
One possibility is for 'range' with no arguments to represent (iterate
inc 0). That would effectively make the default for 'end' infinity:
clojure/range
([] [end] [start end] [start end step])
Returns a lazy seq of nums from start (inclusive) to end
(exclusive), by step, where start defaults to 0, step to 1,
and end to infinity.
Some arguments against:
While this new behavior for an empty argument vector does make a
certain amount of sense as the logical predecessor of "[end]" in the
list of argument vectors, it's possible that seeing:
(range)
in code would not be readily understood as [0, 1, 2, ... ).
There's also the issue that there would be no way to specify this
default value for 'end' explicitly. To address that, one could extend
'range' to understand something like ':infinity' representing "the end
is infinity" or 'nil' as representing "there is no end" as values for
'end'.
Of the options I've described, my favorite is to add support for
"(range)" (range with no arguments) and also to support "nil" as a
valid value for "end", using nil's meaning as "nothing" to represent
the idea that "there is no end". Note that using nil this way would
also naturally support iteration from a value downward without limit
(using a start and a negative step). The equivalent of "(iterate dec
0)" would be:
(range 0 nil -1)
The new docs for range would be (something like):
clojure/range
([] [end] [start end] [start end step])
Returns a lazy seq of nums from start (inclusive) to end
(exclusive), by step, where start defaults to 0, step to 1,
and end to nil. A nil value for end yields an infinite seq.
Does anyone have any thoughts about the usefulness of this idea? Is
"(iterate inc 0)" already short enough and clear enough that this
extension of range is unnecessary? Is there another way to express it
that I've missed? Would an explicit way to say "no end" be useful
enough to warrant adding it to 'range' whether or not range is
extended to take no arguments?
--Steve