Closed
Description
tl;dr
Introduce infix to
(with optional by
) and replace current ranges with it.
intro
The pseudo ranges may look nifty in the first glance, but I think
they've become the bad parts in CoffeeScript.
proposed syntax
i for i in x to y by z
a = [x to y] # can be by-ed as well
# no slice/splice
instead of
i for i in [x .. y] by z
a = [x .. y]
b[x .. y] = c[x .. y]
why current ranges are bad
Because they
- behave wildly different in different contexts.
- array / array comprehension / loop range / slice / splice
- have awkward syntax.
(i for i in [.0 .. .2] by .1)
- Parses as
[(0)...(2)]
. - Looks ugly too.
- Parses as
- generate inefficient code or are inferior syntax sugars.
x = 1; y = 5; [x..y]
- This is because it has to determine the direction dynamically.
to
-by
would have a fixed step and thus generate better code.
- This is because it has to determine the direction dynamically.
- Slicing is broken (or less versatile than
.slice
)'012345'[-3..-1]
- Splicing is broken (and less useful in general)
a = [0..5]; a[1..-2] = [42]
- Ruby
- can be exclusive, but this is a confusing feature (especially with the look). We can live without it by simply subtracting from the right operand (since we only have numeric ranges).
- are confusing in the presence of splatted arrays:
[x...,y]
edit: removed cup links that no longer work.