Closed primo-ppcg closed 12 months ago
I like the first modification. About the other suggestion I am not sure 😊.
I also like the first item.
Not sure about the rest, except that I also felt a negative step would have been nice on occasion. Uncertain about the practicalities though.
I also felt a negative step would have been nice on occasion.
For sure. The problem is the following:
(loop [i :range [10 0 -1]]) # step known to be negative at compile time
(fn [x]
(loop [i :range [10 0 x]])) # step not known to be negative at compile time
It's not really a big issue to check neg?
at runtime, however it's also not known which comparator to use, which will compile to much less effecient code. I suspect this is why :down
exists in the first place.
I say that it would be really nice, not because I think it can be done, but more in hope that someone smarter might have an idea that would work.
Looking at the asymmetry between these forms:
That the second produces an infinite loop is a mistake I confess to having made more than once. The
:range
verb could be modified fairly easily to allow a single term: https://github.com/primo-ppcg/janet/commit/b8e3482619e659f985230756341a9c851dd2c843 Which compiles to the same code as the two or three term forms:This change would also affect
:range-to
,:down
, and:down-to
.Other suggestions
A single term form could also be allowed without needing to be a tuple:
This can be achieved by modifying a single line:
I'm not sure if that's a good idea, just throwing it out there.
It would also be really nice if
:range
could also allow a negative step, asrange
does. I don't know if this could be done without adding some run-time comparisons to the macro expansion, which is definitely undesirable, but if it could I think it would be a good idea. Perhaps the current behavior could be preserved as:up
and:up-to
.