-
Notifications
You must be signed in to change notification settings - Fork 164
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
Implement reversed(seq)
#2644
base: main
Are you sure you want to change the base?
Implement reversed(seq)
#2644
Conversation
@Shaikh-Ubaid I am repurposing the Another way I think is to handle both the cases in one single function. We can call the I request you to guide me on this. |
@Shaikh-Ubaid , the tests for |
@@ -19,6 +19,7 @@ the code size. | |||
|
|||
enum class IntrinsicElementalFunctions : int64_t { | |||
ObjectType, | |||
Reversed, |
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.
Reversed
returns an iterator, but does not actually reverse
a list. I think we do not have support for iterators yet in LPython.
The approach in this PR reverses the entire list, which I think is not the correct approach.
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.
@Shaikh-Ubaid I return a reversed list using the approach used for dict.keys
(#1881 (comment)) as we do not support iterators now.
The current approach in this PR reverses the original list which we should not be doing. As a result, it fails for the following: % git diff
diff --git a/integration_tests/test_builtin_reversed.py b/integration_tests/test_builtin_reversed.py
index 51cf3607f..2f38e2e09 100644
--- a/integration_tests/test_builtin_reversed.py
+++ b/integration_tests/test_builtin_reversed.py
@@ -15,6 +15,7 @@ def test_builtin_reversed():
reversed_numbers: list[i32] = reversed(numbers)
print(reversed_numbers)
assert reversed_numbers == [5, 4, 3, 2, 1]
+ assert numbers == [1, 2, 3, 4, 5]
# list returned through function call
alphabet_dictionary: dict[str, i32] = { % lpython integration_tests/test_builtin_reversed.py
['e', 'd', 'c', 'b', 'a']
[5, 4, 3, 2, 1]
AssertionError |
Thank you very much for catching this @Shaikh-Ubaid ! I totally missed that I was modifying the original list. I think we can create a new function in |
I think we should not do that. Iterators do not make a copy. They simply work like a pointer which can be used to traverse an object. I would not implement |
I think it is correct to either implement support for an iterator or wait for it. 👍 |
Working