Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure range with a specific start and infinite end

Tags:

clojure

According to the docs the range function has four forms:

  • (range) 0 - Infinity
  • (range end) 0 - end
  • (range start end) start - end
  • (range start end step) start - end skipping by step

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?

like image 217
Alex Wayne Avatar asked Aug 17 '15 03:08

Alex Wayne


2 Answers

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

like image 126
Sean Avatar answered Oct 02 '22 12:10

Sean


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.

like image 33
zero323 Avatar answered Oct 02 '22 13:10

zero323