Alex Wayne
Alex Wayne

Reputation: 187114

Clojure range with a specific start and infinite end

According to the docs the range function has four forms:

So how would I declare a range representing x to Infinity?

I may also be asking how do reference infinity, as something like (range x infinity) might work?

Upvotes: 17

Views: 3450

Answers (3)

Alister Lee
Alister Lee

Reputation: 2455

How about:

(defn range-from [start]
   (drop start (range)))

Upvotes: 4

zero323
zero323

Reputation: 330263

How about something like this:

(defn my-range
  ([start] (iterate inc' start))
  ([start step] (iterate #(+' % step) start)))

Please note inc' and +' to support arbitrary precision.

Upvotes: 8

Sean
Sean

Reputation: 29790

(iterate inc x) will give you a lazy, infinite sequence of numbers starting with x.

Upvotes: 31

Related Questions