-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[stdlib] Span slicing with negative step #3650
base: nightly
Are you sure you want to change the base?
[stdlib] Span slicing with negative step #3650
Conversation
Signed-off-by: Manuel Saelices <[email protected]>
Signed-off-by: Manuel Saelices <[email protected]>
stdlib/src/utils/span.mojo
Outdated
dest_ptr[i] = self._data[start] | ||
start -= step | ||
i += 1 | ||
return Span[T, lifetime](unsafe_ptr=dest_ptr, len=new_len) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the best way to ensure the buffer
allocated memory is destroyed when the span is deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, not sure since I don't really understand lifetimes yet. But I think building a var items = List[T](unsafe_pointer=dest_ptr, size=new_len, capacity=new_len)
and returning Span[T, lifetime](items^)
will work?
Either way I think this should probably return an iterator in the future (once we have them), it doesn't make much sense to allocate for this since it is a read only view on the data.
Signed-off-by: Manuel Saelices <[email protected]>
This needs for this PR to be merged into the nightly release, as the negative steps are not working in Mojo: modularml/mojo#3650 Signed-off-by: Manuel Saelices <[email protected]>
|
Yes, it's a little bit weird. However, in Python you can slice a slice, like this: >>> l = list(range(10))[2:5][::-1]
>>> l
[4, 3, 2]
>>> How Mojo could support this kind of operations without the support of negative steps? |
I made a change previously to kinda hack it by giving |
That is amazing! I wonder why this was reverted. To me it's a must to have slicing matching up the Python ones. |
#2950 essentially demonstrates the points I made above: |
What do you think of this proposal #3653 ? |
That's reasonable, I wish we had some context for why that change didn't work internally though. |
Fixes #3649