Skip to content

Python-like slicing #272

@StanAngeloff

Description

@StanAngeloff

I have just pushed a commit that implements Python-like slice literals. The syntax is similar to Python but rather than using colons (which is an assignment in CoffeeScript) it uses commas:

list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
last: list[-1, ]    # last is now [9]

The comma at the end is mandatory to indicate a slice, rather than perform an index lookup. Slicing works on both strings and arrays and it should also work on numbers and objects implementing toString as well:

string: 'Here comes the man!'
every_other_letter: string[,,2]
puts every_other_letter    # prints 'Hr oe h a!'

I am interested to see how many of you find this useful?

There is a downside, however. The current implementation requires about 13 lines of additional JavaScript. I have tried to keep it short and this seems to be the shortest it can get. I have looked at other places in the core where we are using pre-defined functions and there are a few (__hasProp and __extends, 7 lines). In my view, it is a good trade-off between gained functionality and generated JavaScript.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions