jashkenas / jashkenas/coffeescript

Grammar: Add support for stepping to range notation

Open
#3,749 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
CoffeeScript
Stars
16.6k
Forks
2k
PR merge metrics
No merged PRs in 30d

Description

Python has a nifty syntactic sugar for performing stepping in a slice ("extended slices"), that I think should be added to coffeescript.

foo[3:-5] # takes a slice between 3 and 5 from the end
foo[3:-5:2] # takes every 2nd element between 3 and 5 from the end
foo[::2] # takes every 2nd element
foo[::

Obviously, we can't use identical syntax, since coffeescript uses .. and ... for slices instead of :. I'd recommend either using the same operator (:) that python uses or an english word (by)

foo[3..-5:2]
# or
foo[3..-5 by 2]

The 2nd example (using by) is a little odd to my eyes, since it mixes word and symbol operators, so perhaps we should either use a symbol operator (:) and/or add an (optional) word operator for (inclusive) slice/ranges, e.g. to

foo[3 to -5 by 2] is foo[3..-5:2]

For consistency, we would want to add this functionality anywhere that uses range notations, not just slices and splices, but also including for x in [...], and anywhere that I've missed.

More examples, including for in

foo[1..] # 2nd element to the end
foo[...-1] # all but the last element
foo[1...-1] # all but the end elements
foo[:2] # every 2nd element
foo[1...-1:2] # every 2nd element within the slice consisting of everything but the end elements

foo[:2] = replacementsForEvenElements # lengths must match

for x in [10..100:10] # every 10 up to 100

and the same, with word operators:

foo[1..] # 2nd element to the end
foo[...-1] # all but the last element
foo[1 to -2] # all but the end elements
foo[by 2] # every 2nd element
foo[1 to -2 by 2] # every 2nd element within the slice consisting of everything but the end elements

foo[by 2] = replacementsForEvenElements # lengths must match

for x in [10 to 100 by 10] # every 10 up to 100

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue does not name files, tests, or entry points. Begin by reviewing how CoffeeScript parses and compiles range notation, then resolve the proposed operator syntax and its scope across slices, splices, and for-in ranges. Done means the chosen syntax is implemented consistently with coverage for the examples described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
coffeescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.