Skip to content
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] Tuple.__contains__ #2709

Closed
wants to merge 11 commits into from
Closed

Conversation

rd4com
Copy link

@rd4com rd4com commented May 17, 2024

Hello,

here is an implementation of Tuple.__contains__,

it makes it possible to do:

  var x = Tuple(1,2,True)
  if 1 in x: print("x contains 1")

@laszlokindrat suggested a ping for the PR in #2658

@rd4com rd4com requested review from a team as code owners May 17, 2024 14:18
Copy link
Contributor

@laszlokindrat laszlokindrat left a comment

Choose a reason for hiding this comment

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

Pretty cool, thank you!

@rd4com
Copy link
Author

rd4com commented May 17, 2024

@laszlokindrat Yes, sorry, the tests are more complete now, do you see more cases to add ?

There is a test failing in test_list.mojo for macosx, not sure what to do ?

docs/changelog.md Outdated Show resolved Hide resolved
@laszlokindrat laszlokindrat added the imported-internally Signals that a given pull request has been imported internally. label May 17, 2024
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
Signed-off-by: rd4com <144297616+rd4com@users.noreply.github.com>
@rd4com
Copy link
Author

rd4com commented May 19, 2024

The force-push worked with the rebased branch 🥳 :

  • it includes a new @parameter optimization that returns false if T not in self.element_types
    (before var result:Bool = False)
  • no rebind of memory-only types, we rebind the Reference, which is @register_passable

Todo:

  • when @parameter for can do returns, we will be able to return True the first time that self[i] == value,
    instead of continuing doing result |= for every _type_is_eq[element_types[i],T]().
    There would be no need for var result:Bool

ℹ️ Note that the rebase is on 2024.5.1805 not 2024.5.1905

@laszlokindrat
Copy link
Contributor

!sync

Copy link
Contributor

@laszlokindrat laszlokindrat left a comment

Choose a reason for hiding this comment

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

Thanks again! I think this is a useful functionality, and while we can't do it perfectly yet, it does deliver a useful feature, especially for people coming from Python. There are very likely some sharp edges around the current behavior. If you can think of any, please open a PR to document them.

@rd4com
Copy link
Author

rd4com commented May 20, 2024

@laszlokindrat , This is great! this is a cool feature, i'd be happy to use it too 😄 .
 

Once auto-dereference lands:

  • It will probably require some changes, as it might rebind the dereferenced value instead of the Reference.

If there is any way you can add this to sort of a todo list to make sure Tuple.__contains__ still works?

@laszlokindrat
Copy link
Contributor

@laszlokindrat , This is great! this is a cool feature, i'd be happy to use it too 😄 .  

Once auto-dereference lands:

  • It will probably require some changes, as it might rebind the dereferenced value instead of the Reference.

If there is any way you can add this to sort of a todo list to make sure Tuple.__contains__ still works?

The tests should prevent it from being broken.

@modularbot
Copy link
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added merged-internally Indicates that this pull request has been merged internally merged-externally Merged externally in public mojo repo labels May 20, 2024
@modularbot
Copy link
Collaborator

Landed in ea528b3! Thank you for your contribution 🎉

modularbot pushed a commit that referenced this pull request May 21, 2024
[External] [stdlib] Tuple.__contains__

Hello,

here is an implementation of ```Tuple.__contains__```,

it makes it possible to do:
```mojo
  var x = Tuple(1,2,True)
  if 1 in x: print("x contains 1")
```

---------

Co-authored-by: rd4com <144297616+rd4com@users.noreply.github.com>
Closes #2709
MODULAR_ORIG_COMMIT_REV_ID: ec8cf2aaa0912d06f87c2b2ad481a7aa99b4cc03
@modularbot modularbot closed this May 21, 2024
modularbot pushed a commit that referenced this pull request May 23, 2024
[External] [stdlib] Fix `Tuple.__contains__`

Various follow-ups on #2709.

This also ensures `Tuple.__contains__` works in the parameter domain.

Co-authored-by: rd4com <144297616+rd4com@users.noreply.github.com>
Closes #2782
MODULAR_ORIG_COMMIT_REV_ID: 88a3d728bbda276c34ee485d3ee7a57c147ad403
martinvuyk pushed a commit to martinvuyk/mojo that referenced this pull request May 24, 2024
[External] [stdlib] Tuple.__contains__

Hello,

here is an implementation of ```Tuple.__contains__```,

it makes it possible to do:
```mojo
  var x = Tuple(1,2,True)
  if 1 in x: print("x contains 1")
```

---------

Co-authored-by: rd4com <144297616+rd4com@users.noreply.github.com>
Closes modularml#2709
MODULAR_ORIG_COMMIT_REV_ID: ec8cf2aaa0912d06f87c2b2ad481a7aa99b4cc03
martinvuyk pushed a commit to martinvuyk/mojo that referenced this pull request May 24, 2024
[External] [stdlib] Fix `Tuple.__contains__`

Various follow-ups on modularml#2709.

This also ensures `Tuple.__contains__` works in the parameter domain.

Co-authored-by: rd4com <144297616+rd4com@users.noreply.github.com>
Closes modularml#2782
MODULAR_ORIG_COMMIT_REV_ID: 88a3d728bbda276c34ee485d3ee7a57c147ad403
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants