janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.43k stars 221 forks source link

Allow one-term `:range` and `:down` forms #1278

Closed primo-ppcg closed 12 months ago

primo-ppcg commented 12 months ago

Related: #1275

Makes :range more predictable, in particular that:

(loop [i :range [10]] body)

is equivalent to:

(loop [i :range [0 10] body)

rather than producing an infinite loop.

This is consistent with the behavior that the following forms:

(range 10)
(range 0 10)

are also equivalent.

sogaiu commented 12 months ago

Tried it out with the usual set of tests and no issues were detected :+1:

...and some manual trials (edited for readability):

$ ./build/janet
Janet 1.30.0-2f43cb84 linux/x64/gcc - '(doc)' for help

repl:1:> (seq [i :range [0 10]] i)
@[0 1 2 3 4 5 6 7 8 9]

repl:2:> (seq [i :range [10]] i)
@[0 1 2 3 4 5 6 7 8 9]

repl:3:> (seq [i :range-to [10]] i)
@[0 1 2 3 4 5 6 7 8 9 10]

repl:4:> (seq [i :down [10]] i)
@[10 9 8 7 6 5 4 3 2 1]

repl:5:> (seq [i :down-to [10]] i)
@[10 9 8 7 6 5 4 3 2 1 0]

Having tried out the down ones, I did find myself pausing for a moment about their meanings. I understand about the symmetry angle, just giving some impressions here :)