Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: Python-like slices in vec #1799
Comments
This comment has been minimized.
This comment has been minimized.
BrendanEich
commented
Feb 10, 2012
|
+1 all around. Thanks, /be Niko Matsakis wrote:
|
This comment has been minimized.
This comment has been minimized.
BrendanEich
commented
Feb 10, 2012
|
Oops, replied to the list. For the record, +1 all around. /be |
This comment has been minimized.
This comment has been minimized.
|
Looks good. Why not make islice into slice? |
This comment has been minimized.
This comment has been minimized.
|
I agree with killerswan. Just call |
This comment has been minimized.
This comment has been minimized.
|
I'd make the unsigned-integer version fail on out-of-range indices. So you'd have the choice of a liberal version and a strict version. Also, yes, name one of them simply |
This comment has been minimized.
This comment has been minimized.
|
I'd be ok with |
This comment has been minimized.
This comment has been minimized.
qwertie
commented
Feb 10, 2012
|
As a general principle, I think that for performance reasons, there should always be a "never-fails" version of all important language primitives. The reason is that it is common for developers to want a version that can't fail, and if the language/library doesn't offer one then the developer has to write a function to do it himself. Then what happens is, the developer checks the arguments, and then the compiler or std lib does exactly the same check a second time, which is just wasteful in an inner loop. Plus, the compiler has to spend a little extra time inlining this little function everywhere. Of course, a version that fails on unexpected input is equally useful and should also be provided. IMO, then, there should be three slice functions: two fast (unsigned with fail, unsigned without fail) and one slower and more flexible (python style). But then you would have more arguing to do about naming. Mind you, I didn't catch how slices work in Rust -- I'm assuming it's like Go where a slice is merely a pointer and a length, no copying involved. If a slice actually copies the whole range then it was silly of me to worry about the speed of the range checks. |
This comment has been minimized.
This comment has been minimized.
|
One thing I like about python is that it has a single slice operator (albeit with optional arguments). I find an API made up of lots of very similar functions to be confusing. |
This comment has been minimized.
This comment has been minimized.
|
Python's slicing mechanism really is one of my favorite aspects of the language. +1 Add my voice to the chorus of those who'd love to see the It looks like Go handles this by making slices a separate type entirely: http://blog.golang.org/2011/01/go-slices-usage-and-internals.html This issue is also related to #555. |
This comment has been minimized.
This comment has been minimized.
nettok
commented
Feb 14, 2012
|
Don't forget about Python's extended slice notation With this is possible to reverse a list doing this |
This comment has been minimized.
This comment has been minimized.
|
+1 for [from:to:step] syntax. |
This comment has been minimized.
This comment has been minimized.
|
Why not just call vec::reverse? |
This comment has been minimized.
This comment has been minimized.
nettok
commented
Feb 14, 2012
|
vec::reverse would be equivalent to For example, take only pair indices |
This comment has been minimized.
This comment has been minimized.
|
There was some gesture towards supporting this long ago, but I removed it out of lack of effort. If someone wants to champion it, I'm fine with that. |
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis, |
This comment has been minimized.
This comment has been minimized.
amunra
commented
Mar 17, 2012
|
+1 I'd love to see the slicing notation
Makes for very intuitive code |
ghost
assigned
graydon
Apr 12, 2012
This comment has been minimized.
This comment has been minimized.
|
Underway, but not for 0.3 and possibly not with |
This comment has been minimized.
This comment has been minimized.
|
withdrawing in favor of a newer proposal based on our current slices |
nikomatsakis
closed this
Jun 1, 2012
This comment has been minimized.
This comment has been minimized.
|
I take it that means this proposal: http://smallcultfollowing.com/babysteps/blog/2012/05/14/vectors/ Not sure if there's an issue for it yet. |
nikomatsakis commentedFeb 10, 2012
I would like to modify the vector (and string, I suppose) slicing routines to be more "Python-like". This means that they are more lenient of invalid indices and they consider negative numbers to count from the right.
All in all I propose six functions:
The
ifamily accepts signed integers: negative inputs are considered as counting from the right. Theufamily takes unsigned integers. All of them are tolerant of invalid indices, returning empty list rather than failing, as Python does. In my experience this is usually what you want.