Skip to content

Commit 497ffea

Browse files
authored
Fix higher arities of the range function (#1006)
Fixes #1004
1 parent 510a7b5 commit 497ffea

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
* Fix an issue with `basilisp.core/time` failing when called outside `basilisp.core` (#991)
2525
* Fix an issue with `basilisp.core/promise` where a thread waiting for a value from another thread might not wake up immediately upon delivery (#983).
2626
* Fix using keyword as a function not returning the default value in some cases (#997)
27-
* Fix an issue where Python `SyntaxWarning`s would be emitted for certain compiled code (#???)
27+
* Fix an issue where Python `SyntaxWarning`s would be emitted for certain compiled code (#996)
28+
* Fix an issue where 2- and 3-arity `range` invocations returned unexpected results (#1004)
2829

2930
### Removed
3031
* Removed `python-dateutil` and `readerwriterlock` as dependencies, switching to standard library components instead (#976)

src/basilisp/core.lpy

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,18 +2523,21 @@
25232523

25242524
(defn range
25252525
"Return a range of integers from ``start``\\. If ``end`` is specified, the sequence
2526-
will terminate at ``end``\\."
2526+
will terminate at ``end``\\. If ``step`` is specified, that amount will be added
2527+
for each iteration. ``step`` may be negative."
25272528
([]
25282529
(iterate inc 0))
25292530
([end]
25302531
(lazy-seq (cons 0 (range 1 end))))
25312532
([start end]
2532-
(lazy-seq (cons start (when (< start (dec end))
2533-
(range (inc start) end)))))
2533+
(lazy-seq
2534+
(when (< start end)
2535+
(cons start (range (inc start) end)))))
25342536
([start end step]
2535-
(lazy-seq (let [next-int (+ start step)]
2536-
(cons start (when (< next-int (dec end))
2537-
(range next-int end step)))))))
2537+
(lazy-seq
2538+
(when (or (and (>= step 0) (< start end))
2539+
(and (< step 0) (> start end)))
2540+
(cons start (range (+ start step) end step))))))
25382541

25392542
(defn complement
25402543
"Return a function which returns the logical complement of the return value of

tests/basilisp/core_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,10 +1370,10 @@ def test_select_keys(self):
13701370

13711371

13721372
def test_range():
1373-
assert llist.l(1) == core.range_(1, 1)
1373+
assert llist.EMPTY == core.range_(1, 1)
13741374
assert llist.l(1, 2, 3, 4, 5) == core.range_(1, 6)
13751375
assert llist.l(1, 3, 5, 7, 9) == core.range_(1, 11, 2)
1376-
# assert llist.l(1, -1, -3, -5, -7, -9) == core.range_(1, -10, -2)
1376+
assert llist.l(1, -1, -3, -5, -7, -9) == core.range_(1, -10, -2)
13771377
assert 9999 == len(core.vec(core.range_(1, 10000)))
13781378

13791379

tests/basilisp/test_core_fns.lpy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,26 @@
710710
(run! #(swap! a + %) (range 5))
711711
(is (= @a 10)))))
712712

713+
(deftest range-test
714+
(testing "0-arity"
715+
(is (= '(0 1 2 3 4) (take 5 (range)))))
716+
717+
(testing "1-arity"
718+
(is (= '(0 1 2) (take 5 (range 3))))
719+
(is (= '(0 1 2) (range 3)))
720+
(is (= '(0 1 2 3 4) (take 5 (range 10)))))
721+
722+
(testing "2-arity"
723+
(is (= '() (range 3 3)))
724+
(is (= '(1 2 3) (range 1 4)))
725+
(is (= '(1 2 3 4 5) (take 5 (range 1 10)))))
726+
727+
(testing "3-arity"
728+
(is (= '() (range 3 3 2)))
729+
(is (= '(1 4 7) (range 1 10 3)))
730+
(is (= '(3 2 1) (range 3 0 -1)))
731+
(is (= '(10 7 4 1) (range 10 0 -3)))))
732+
713733
(deftest partial-test
714734
(testing "no arguments"
715735
(let [f-no-args (fn [] :no-args)]

0 commit comments

Comments
 (0)