I don't think most people who are using Go really want to write their own contains method for slices every time: it seems antithetical to being productive and doing what you actually want to do and it is most definitely a shortcoming of the language. Fortunately with generics people can hopefully need to do that a lot less!
Just in case you forgot: Contains is a (nearly) standard lib function these days. Go did get generics, which are type safe (unlike some other languages I could mention). Before that, you had to write a slightly more clunky function yourself, but it was still a one-liner.
Second: it's not a good function to use. Checking if something is present in an array is not something you should do often and thoughtlessly. It has its uses, I agree, but in general a "dict", or if you really need an array, binary search, should be used. Where's the one liner for that in python? O wait, there isn't one. Go does have it, though, since a long time.
But it's a good thing we no longer use LoC as a measure.
Note that that finds the index to the left of the insertion point for the value given, but doesn’t perform a containment test; you have to also check that the index is in the list (instead of off the end) and that the value at the resulting index equals the value of interest to test containment. Which can be a one-liner without redundant calls (with walrus), but is a bit ugly.
(Of course, for a custom class you can just wrap it in a __contains__ method and then just use the “in” operator.)
> you have to also check that the index is in the list (instead of off the end) and that the value at the resulting index equals the value of interest to test containment
True. However, the hardest part of the binary search algorithm is implemented through bisect, so it still saves a lot of developer time.
You can do that too, using the bisect, provided you have the __len__ and __getitem__ implemented:
class C:
def __len__(self): ...
def __getitem__(self, i): ...
def __contains__(self, x):
i = bisect.bisect_left(self, x)
return i < len(self) and self[i] == x
The first two lines are setup, real code would already have done that.
You could maybe count the import, but you'd have to do that once and could do multiple bisections, so it's amortized.
Setting the variable doesn't count because you do, of course, need a variable to perform an operation on a variable. Sorting the array also wouldn't count because you can't use binary search if the array isn't sorted.
I don't know Go, however checking if an element is in an array, even unsorted, is likely faster than anything involving a linked list or a hash map unless the equality test is very expensive (maybe) if the array is small enough. And it is often small enough. I don't know how small, I guess it depends on the computer and the implementation, but 100 is probably still fine.
So, yes, let's have a contains function on (unsorted) arrays.
Are you really shaming us for wanting to check the contents of an array? god damn.. how can you get anything useful done, or even meet the requirements? What you are proposing goes beyond having your hands tied and being blindfolded.
Loc is a bad code metric when measuring at the scale of codebases. However, when measuring the succinctness of a language it has its place.
And, contains is a perfectly cromulent function to use unless there's a reason not to. At a million items it would be a bad use of contains if you were to lookup up multiple items, but modern day programming requires both knowledge of the code structure and the data.
Yes, python's type hinting is pants.
Go's goals are simplicity, python wants to be the working man's language.
But then yea, 1 line of python using an inbuilt function solves a 10 line go routine in a hackerrank.