-
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] Implement __contains__
for Tuple
, List
, ListLiteral
(almost)
#2658
Comments
Tried something like this: struct Bucket[T: CollectionElement]:
var elements: List[T]
fn __init__(inout self, elements: List[T]):
self.elements = elements
@staticmethod
fn __contains__[C: ComparableCollectionElement](self: Bucket[C], borrowed elem: C) -> Bool:
for i in range(self.elements.size):
if elem.__eq__(self.elements[i]):
return True
return False However, i'm getting: error: special method may not be a static method
fn __contains__[C: ComparableCollectionElement](self: Bucket[C], borrowed elem: C) -> Bool:
^
mojo: error: failed to parse the provided Mojo source module If I remove error: 'self' argument must have type 'Bucket[T]', but actually has type 'Bucket[C]'
fn __contains__[C: ComparableCollectionElement](self: Bucket[C], borrowed elem: C) -> Bool:
^ ~~~~~~~~~
mojo: error: failed to parse the provided Mojo source module |
Is there something similar to c++ |
@laszlokindrat , @ChristopherLR , if self can be a reference? this parametrization could do the job: struct Bucket[T: CollectionElement]:
var elements: List[T]
fn __init__(inout self):
self.elements = List[T]()
fn __contains__[C: ComparableCollectionElement](
self: Reference[Bucket[C]], elem: C
) -> Bool:
for i in range(self[].elements.size):
if elem.__eq__(self[].elements[i]):
return True
return False
fn main():
var x = Bucket[Int]()
x.elements.append(1)
if x.__contains__(1):
print("ok")
#if 1 in x: #not yet |
Or if we don't use a special method: struct Bucket[T: CollectionElement]:
var elements: List[T]
fn __init__(inout self, elements: List[T]):
self.elements = elements
@staticmethod
fn contains[C: ComparableCollectionElement](self: Bucket[C], value: C) -> Bool:
for i in range(self.elements.size):
if value.__eq__(self.elements[i]):
return True
return False
fn main():
var b = Bucket[Int](List(1, 2, 3))
if __type_of(b).contains(b, 2):
print("2 is in the bucket")
else:
print("2 is not in the bucket") |
@rd4com Very cool, and I am actually surprised that parameter inference fails for the |
@rd4com I used your trick on Would you be interested in going ahead and implementing Regarding the |
@laszlokindrat Nice,
I'll try to implement Of course, you're welcome to make theses PR's @ChristopherLR aswel (and any other person too)! |
Following @rd4com's suggestion in modularml#2658, this method can take a reference type as self, allowing the callsite to use a natural syntax. This patch also adds a changelog entry for `List.index`. MODULAR_ORIG_COMMIT_REV_ID: 291dd5f544d7d5b463188e7c986b2ee3c8416662 Signed-off-by: Lukas Hermann <[email protected]>
Hey @rd4com and @laszlokindrat, not quite |
Should we even provide |
@rd4com I discovered that this test fails for def test_list_contains_str():
var x = List[String]("a", "b", "c")
assert_false("d" in x)
assert_true(x.__contains__("a"))
assert_false(x.__contains__("e"))
However this passes: def test_list_contains_str():
var x = List[String]("a", "b", "c")
assert_true(x.__contains__("a"))
assert_false(x.__contains__("e")) Note that i've delete |
Yeah, this is the parser bug I mentioned before. Don't worry about it, direct call to |
It would be nice if we could do |
@laszlokindrat I agree that it's rather convenient. But shouldn't we write |
Can you give an example on how this could be misused/abused? I feel pretty strongly that, from a "python superset" point of view, |
The concern here isn't misuse, but rather whether we need to mimic Python in this particular case. Python's
In a typed language like Mojo, it's logical for the latter two types to implement this interface. Moreover, a Python |
Syntactically,
Now this is a good point; maybe the problem is that |
Totally, if we can enforce the same type requirement at compile time.
Again, agreed. Question: should we allow |
…tains__ to InlineList (#40189) [External] [mojo-stdlib] Add variadic initialiser, __iter__ and __contains__ to InlineList This PR adds some features to InlineList ( related issue #2658 ) *Variadic initialiser* ```mojo var x = InlineList[Int](1,2,3) ``` *iter* ```mojo var x = InlineList[Int](1,2,3) for i in x: print(i) ``` *contains* ```mojo var x = InlineList[Int](1,2,3) if 3 in x: print("ok") ``` Co-authored-by: Chris <[email protected]> Closes #2703 MODULAR_ORIG_COMMIT_REV_ID: 6a9eb7b41905e62f8db0855159fd851b7ffb6174
Following @rd4com's suggestion in modularml#2658, this method can take a reference type as self, allowing the callsite to use a natural syntax. This patch also adds a changelog entry for `List.index`. MODULAR_ORIG_COMMIT_REV_ID: 291dd5f544d7d5b463188e7c986b2ee3c8416662
…tains__ to InlineList (#40189) [External] [mojo-stdlib] Add variadic initialiser, __iter__ and __contains__ to InlineList This PR adds some features to InlineList ( related issue modularml#2658 ) *Variadic initialiser* ```mojo var x = InlineList[Int](1,2,3) ``` *iter* ```mojo var x = InlineList[Int](1,2,3) for i in x: print(i) ``` *contains* ```mojo var x = InlineList[Int](1,2,3) if 3 in x: print("ok") ``` Co-authored-by: Chris <[email protected]> Closes modularml#2703 MODULAR_ORIG_COMMIT_REV_ID: 6a9eb7b41905e62f8db0855159fd851b7ffb6174
I don't see why not. A perfect implementation of |
Following @rd4com's suggestion in modularml#2658, this method can take a reference type as self, allowing the callsite to use a natural syntax. This patch also adds a changelog entry for `List.index`. MODULAR_ORIG_COMMIT_REV_ID: 291dd5f544d7d5b463188e7c986b2ee3c8416662
…tains__ to InlineList (#40189) [External] [mojo-stdlib] Add variadic initialiser, __iter__ and __contains__ to InlineList This PR adds some features to InlineList ( related issue modularml#2658 ) *Variadic initialiser* ```mojo var x = InlineList[Int](1,2,3) ``` *iter* ```mojo var x = InlineList[Int](1,2,3) for i in x: print(i) ``` *contains* ```mojo var x = InlineList[Int](1,2,3) if 3 in x: print("ok") ``` Co-authored-by: Chris <[email protected]> Closes modularml#2703 MODULAR_ORIG_COMMIT_REV_ID: 6a9eb7b41905e62f8db0855159fd851b7ffb6174
…tains__ to InlineList (#40189) [External] [mojo-stdlib] Add variadic initialiser, __iter__ and __contains__ to InlineList This PR adds some features to InlineList ( related issue #2658 ) *Variadic initialiser* ```mojo var x = InlineList[Int](1,2,3) ``` *iter* ```mojo var x = InlineList[Int](1,2,3) for i in x: print(i) ``` *contains* ```mojo var x = InlineList[Int](1,2,3) if 3 in x: print("ok") ``` Co-authored-by: Chris <[email protected]> Closes #2703 MODULAR_ORIG_COMMIT_REV_ID: 6a9eb7b41905e62f8db0855159fd851b7ffb6174
[External] [stdlib] `__contains__` for `ListLiteral` An implementation of the `__contains__` function for `ListLiteral` as per issue #2658. The function parameterizes the `EqualityComparable` trait, this maintains consistency with `Tuple.__contains__` behavior (in nightly). ORIGINAL_AUTHOR=Joshua James Venter <[email protected]> PUBLIC_PR_LINK=#3251 Co-authored-by: Joshua James Venter <[email protected]> Closes #3251 MODULAR_ORIG_COMMIT_REV_ID: 2ebceae2217a745fc10d8a61205c2cad5220969d
[External] [stdlib] `__contains__` for `ListLiteral` An implementation of the `__contains__` function for `ListLiteral` as per issue #2658. The function parameterizes the `EqualityComparable` trait, this maintains consistency with `Tuple.__contains__` behavior (in nightly). ORIGINAL_AUTHOR=Joshua James Venter <[email protected]> PUBLIC_PR_LINK=#3251 Co-authored-by: Joshua James Venter <[email protected]> Closes #3251 MODULAR_ORIG_COMMIT_REV_ID: 2ebceae2217a745fc10d8a61205c2cad5220969d
Now that we have
ComparableCollectionElement
, we can try to implement__contains__
for some common collection types using a workaround similar to what was employed in #2190. It's possible that the compiler would be able to correctly pick up a__contains__
overload with@staticmethod
, e.g. something like this might just work:Even if
in
doesn't work with this, the implementation shouldn't have to change much once we roll out conditional conformance.The text was updated successfully, but these errors were encountered: