Skip to content

Add list.count #1676

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

Merged
merged 3 commits into from
Apr 9, 2023
Merged

Add list.count #1676

merged 3 commits into from
Apr 9, 2023

Conversation

virendrakabra14
Copy link
Contributor

Adds list.count method. Completes a part of #941.

  1. For incompatible types, an error is raised. Should we return count 0 instead?
  2. I am not sure in which file to add the test-cases, but I tried the following test locally:
def test_List():
    a: list[i32]
    a = [1, 2, 3]

    # print(a.count(1.0)) # semantic error: Type mismatch in 'count', the types must be compatible
    print(a.count(-1))
    print(a.count(2))
    a.append(2)
    print(a.count(2))
    a.remove(2)
    print(a.count(2))

test_List()
0
1
2
1

@virendrakabra14 virendrakabra14 marked this pull request as ready for review April 7, 2023 07:48
Copy link
Collaborator

@czgdp1807 czgdp1807 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Please add an integration test as well for list.count. See test_list_*.py in https://github.com/lcompilers/lpython/tree/main/integration_tests for the pattern of testing lists and use your creativity for writing strong test for list.count (for example l.count where l can be empty, check l.count after every insertion/deletion in a loop, etc.)

@czgdp1807 czgdp1807 marked this pull request as draft April 7, 2023 09:31
@czgdp1807
Copy link
Collaborator

  1. For incompatible types, an error is raised. Should we return count 0 instead?

I would prefer an error. For testing error see https://github.com/lcompilers/lpython/tree/main/tests/errors and following pattern for registering error tests,

lpython/tests/tests.toml

Lines 66 to 68 in b35e744

[[test]]
filename = "errors/type_mismatch.py"
asr = true

@Shaikh-Ubaid
Copy link
Collaborator

I think your commits are including previous changes (already merged/present in latest main branch).

@Shaikh-Ubaid
Copy link
Collaborator

To remove extra commits (already present in main), you can rebase over main branch.

For example, first update your main branch:

git checkout main
git pull

Now switch to your branch and rebase:

git checkout scratch
git rebase main

Now force push it

git push your_remote scratch --force-with-lease

@virendrakabra14
Copy link
Contributor Author

Thanks @Shaikh-Ubaid. Will use this in future.

@virendrakabra14 virendrakabra14 marked this pull request as ready for review April 8, 2023 04:21

assert x == [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]

while len(x)>0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while len(x)>0:
while len(x) > 0:


llvm::Value* left_arg = read_item(list, LLVM::CreateLoad(*builder, i),
false, module, LLVM::is_llvm_struct(item_type));
llvm::Value* cond = llvm_utils->is_equal_by_value(left_arg, item, module, item_type);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good that you figured it out on your own.

@czgdp1807 czgdp1807 marked this pull request as draft April 8, 2023 06:24
@czgdp1807 czgdp1807 marked this pull request as ready for review April 9, 2023 17:53
llvm_utils->start_new_block(loopend);

return LLVM::CreateLoad(*builder, cnt);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now this is good. Later we should lift this into ASR and implement there, it will be a lot easier to maintain.

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, thanks!

@certik certik dismissed czgdp1807’s stale review April 9, 2023 22:00

It has been addressed.

@certik certik merged commit 1efcc7f into lcompilers:main Apr 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants