Request for discussion: suggest default 'end' of "infinity" for 'range'; a way to express "no limit"

138 views
Skip to first unread message

Stephen C. Gilardi

unread,
Jul 13, 2008, 10:05:30 PM7/13/08
to clo...@googlegroups.com
I've noticed that the phrase "(iterate inc 0)" comes up noticeably
often in Clojure code dealing with lazy sequences of indefinite length.

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

timmy

unread,
Jul 14, 2008, 8:02:39 AM7/14/08
to Clojure
Hi,

On 14 Jul., 04:05, "Stephen C. Gilardi" <scgila...@gmail.com> wrote:
> I've noticed that the phrase "(iterate inc 0)" comes up noticeably
> often in Clojure code dealing with lazy sequences of indefinite length.

I encountered the same "pattern" of needing an enumerating sequence of
infinite length. Two unsatisfying approaches: (range 0 999999) and
(iterate inc 0) led me to writing my own infinite-range function:

(defn counter
"like range but always infinite. Defaults to (= start step 0)."
([] (iterate inc 0))
([start] (iterate inc start))
([start step] (iterate #(+ % step) start)))


> 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?

No, i don't like (iterate inc 0) when using it in this very general
case of generating (1 2 3 ... inf). I would prefer to explicitly say
"no end", either through another function-name like the above
"counter" or "infinite-range" or "irange" or by setting the end in
range to :inf, so that it is possible to generate infinite ranges with
a start and/or step different from 0/1 using the range function.

--timmy



Reply all
Reply to author
Forward
0 new messages