1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-26 00:10:27 +00:00

Add changes and test cases for #1324

This commit is contained in:
Calvin Rose 2023-11-09 11:18:03 -06:00
parent 715eb69d92
commit a3228f4997
2 changed files with 18 additions and 4 deletions

View File

@ -420,10 +420,14 @@
(defn- range-template (defn- range-template
[binding object kind rest op comparison] [binding object kind rest op comparison]
(let [[start stop step] (check-indexed object)] (check-indexed object)
(case kind (def [a b c] object)
:range (for-template binding (if stop start 0) (or stop start) (or step 1) comparison op [rest]) (def [start stop step]
:down (for-template binding start (or stop 0) (or step 1) comparison op [rest])))) (case (length object)
1 (case kind :range [0 a 1] :down [a 0 1])
2 [a b 1]
[a b c]))
(for-template binding start stop step comparison op [rest]))
(defn- each-template (defn- each-template
[binding inx kind body] [binding inx kind body]

View File

@ -241,6 +241,16 @@
(assert (pos? (% x 4)) "generate in loop")) (assert (pos? (% x 4)) "generate in loop"))
(assert (= gencount 75) "generate loop count") (assert (= gencount 75) "generate loop count")
# more loop checks
(assert (deep= (seq [i :range [0 10]] i) @[0 1 2 3 4 5 6 7 8 9]) "seq 1")
(assert (deep= (seq [i :range [0 10 2]] i) @[0 2 4 6 8]) "seq 2")
(assert (deep= (seq [i :range [10]] i) @[0 1 2 3 4 5 6 7 8 9]) "seq 3")
(assert (deep= (seq [i :range-to [10]] i) @[0 1 2 3 4 5 6 7 8 9 10]) "seq 4")
(def gen (generate [x :range-to [0 nil 2]] x))
(assert (deep= (take 5 gen) @[0 2 4 6 8]) "generate nil limit")
(def gen (generate [x :range [0 nil 2]] x))
(assert (deep= (take 5 gen) @[0 2 4 6 8]) "generate nil limit 2")
# Even and odd # Even and odd
# ff163a5ae # ff163a5ae
(assert (odd? 9) "odd? 1") (assert (odd? 9) "odd? 1")