mojo
mojo copied to clipboard
[stdlib] Add `count(List, value)` method
Similar to Python's list.count(x)
method, we should have the same for our List
. It can't be a member method yet due to lack of conditional conformance in requiring the __eq__
trait (it's not present in CollectionElement
today).
As I suggest in https://github.com/modularml/mojo/pull/2190#issuecomment-2062101102, we could add this as a free function since we don't have conditional conformance e.g.
trait ComparableCollectionElement(EqualityComparable, CollectionElement):
pass
def count[T: ComparableCollectionElement](arg: List[T], value: T):
...
in list.mojo
.
I think this would require adding EqualityComparable
to CollectionElement
, and/or conditional conformance, right?
I think this would require adding
EqualityComparable
toCollectionElement
, and/or conditional conformance, right?
Ah, yes. I removed the "good first issue" as we can't do this yet. Thanks.
Can you provide some code reference?
As I suggest in https://github.com/modularml/mojo/pull/2190#issuecomment-2062101102, we could add this as a free function since we don't have conditional conformance e.g.
def count[T: CollectionElement](arg: List[T], value: T):
...
in list.mojo
. So, marking this back as "good first issue" since it can be implemented today.
Can you provide some code reference?
Of course. This is referring to the List.count(x)
method in Python - see the docs.
Example code in Python
xs = [42, 100, 100]
xs.count(42) # 1
xs.count(100) # 2
xs.count(23) # 0
Can you assign this to me @JoeLoser
Can you assign this to me @JoeLoser
Done!
@JoeLoser Should I wait for #2190 to be merged before taking this up again? Also a small doubt: The value for each of the element is a mlir type right? How do we compare the mlir type with value?