Skip to content

Remove Ranges, introduce "from x to y" #746

Closed
@satyr

Description

@satyr

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.
  • 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.
    • 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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions