-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python-like slicing #272
Comments
It's certainly an often used thing. Maybe there's another syntax to avoid colloision with index lookup? E.g I don't think
|
I find this incredibly useful. I've always found One thing: this, if accepted, should replace the other slicing construct to avoid confusion. Then, maybe, the added utility function could be just as large as it needs to be, somehow. I think we should stick to the traditional |
I agree it's very useful. Perhaps a better syntax would be : string[1..2] string[..-1] string[-1..] Also: is it worth the extra complexity to allow a stepwise parameter ? - it's strikes me that it's not often used. |
weepy, I was going for this syntax when I started the patch, but since it's a complete rewrite on
At one point I had some very basic optimisations, when |
how about |
I was just keen to know how that issue was progressing. I'd really like to see it into trunk one day. Also, I'm wondering if the negative step could be replaced by just comparing the first and second slice argument (if the first is greater, then assumed negative walk through the list). |
I have been using this feature on my slice2 brach for about a week. It's pretty darn nice, but I am still not sure it's core material. It would require three additional functions
Where So to recap, I am not sure whether it is worth patching up the slice2 branch since it may never land in the core. I am pretty happy with what I've got there at the moment. |
good for an extension ? |
If we get the wrapping of utility functions accepted, adding those 3 would not be a problem, I think, as simple clean functions, such as weepy: An extension could never distinguish between its use as a variable to which something is assigned (when splicing) and when it needs its values (slicing). |
Stan and I talked about this a week or so ago. It would be great to get into core, but we need a slightly different implementation that helps clean up our internals. The fundamental issue is passing a So a |
How about this: __range: (object, start, end, xclusive) -> L: object.length start: or 0 end: or L - 1 start: + L if start < 0 end: + L if end < 0 e: - 1 if xclusive [start, end] a :[1,2,3,4,5] __range a, 0, 3 # [0,3] __range a, 0, -1 # [0,4] __range a, -1, -2 # [4,3] __range a, undefined, -2 # [0,3] __range a, undefined, undefined # [0,4] __range a, 0, 2, true # [0,1] |
The
|
Here, I have a branch that contains your recent work, with the factoring out of |
Stan and Matehat's branches are now merged to master, but a certain amount of the work has been reverted. The
Check out the compare view to see the changes from the branches that are now on master: http://github.com/jashkenas/coffee-script/compare/a934cf4...master Closing the ticket. |
Is array[4..] gone as well ? |
So... consistency? I have just pushed two commits to my slice3 branch that deal with slicing, splicing and indexing using negative offsets.
You can see the generated JavaScript in my terminal gist. The soak operations are not very smart and may leak temporary variables, but at this time of night I am too tired to fix it. Help would be appreciated. So, what do you think? |
My concern about this is to see how it would handle negative variables. For consistency, we would need to allow them, but then, we'd need to check for variable sign every time. Here's a quick benchmark comparing a loop with automatic sign checking (named 'smart') and one without (named 'dumb'). |
@matehat, there is no easy and quick way to allow negative variables. |
But then, we'd have to tell then that there's only one way to use negative index and that's when typing If Jeremy is willing to push this to trunk, I'd be jumping with joy :) |
We had a short discussing on IRC last night (for me) -- either way I am happy. I have it in my branch so trunk or no trunk, it's still going to be used extensively by me. |
what came out of that discussion? |
Is it possible to turn this into an extension ? So you can opt-in easily on a file by file basis ? |
I can't say anything came out of it really, it's just we had a brief chat and decided to re-open the ticket to see what the overall feeling about this feature is.
I am afraid the patches are too complicated to work as an extension. I have some basic ideas for future optimisations which I might try and get to the branch at some point, but even in it's current state and with the explicit syntax, it should be fast enough in most cases. I am using it in my code and I am happy with the results. |
As much as I love negative indices, and the current compiler would benefit from them in many cases, I still don't see how we could have a language where...
Works, but...
... does not work. (And injecting extra code into every indexing lookup isn't really acceptable.) This is just one of those things that needs to be supported at the JavaScript VM level, and that's all there is to it. |
Fair enough. Could we still implement |
We certainly could. Do you think it's problematic that it's ambiguous with splats?
... would expand the
... would not. Is that acceptable? If it is, I'd be glad to take the patch. |
+1 for Is |
I'm open to suggestions about the syntax -- is there anything we can do that would look good and still allow us to have negative indices? I am thinking:
If we all like the feature so much, let's all think of a way we can get around JS limitations. If this is clearly not possible, well it's clearly not happening :D |
This is why the grammar does not support
It certainly is in cases where you would like to duplicate an array. It might work on |
jashkenas: semantically, I think the difference between lookup and argument passing is strong enough to break the ambiguity. Also, only one of |
Agree matehat; to my eyes, in line with the rest of the slice params, I read the following: In this context I'd say that |
Given the ambiguity of with splats, and given that it breaks when used as range literals, I think this ticket should be closed again. If you can do Also, if I can use a range to slice an array:
So, going back to |
Mmmm, I think it's a bit early to give up on something that could be really great and that is just a few steps from being consistent. I agree there's a consistency problem with ranges and I say: let's just split them apart. Range literals don't have to look the same as index slicing, do they? What if our slicing was more python-style then ruby-style : I find indefinite end far more useful that having the possibility to include/exclude the last index. What do you think? |
I don't know if I've been clear on that but my point is :
|
Sure, if there's a really wonderful proposal or patch, then that's great. It's just a really tiny feature, and we shouldn't sacrifice consistency, clarity, or simplicity for it ... the negative index problem being a good example of a solution being more work to keep straight than the problem it intends to solve. Part of being "a little language" is choosing your battles. |
I don't think that last proposal is anywhere close to hurt consistency, clarity or simplicity, does it? It's been at work and makes sense for thousands of people developing with python. IMO, the fact that the above implied an ambiguity between range literals and array slicing showed that the parallelism between the two construct was anything but adequate. |
matehat: agreed, but it's quite a different idea. Feel free to open a new ticket for it if you like. |
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:
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: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.The text was updated successfully, but these errors were encountered: