Range is exclusive of the end argument. For example:
> (range 1 3)
(1 2)
I'd like a function that's like range, but is inclusive of the end state:
> (inclusive-range 1 3)
(1 2 3)
Unfortunately, this isn't the same as adding the end to the seq returned by range. For example:
> (inclusive-range 1 2.5)
(1 2)
I don't believe there's anything directly in Clojure that offers this, but I've found it handy. I've written a function that works (but is not lazy).
(defn inclusive-range
"Return a sequence of nums from START to END, both inclusive, by STEP."
([start end]
(inclusive-range start end 1))
([start end step]
(let [test-for-done (if (< start end)
>
<)]
(if (test-for-done start (+ start step))
[]
(loop [nums []
cur start]
(if (test-for-done cur end)
nums
(recur (conj nums cur)
(+ cur step))))))))
Am I missing a better way to do this? Is this something that could be added to Clojure?