bearophile wrote: > Nick Sabalausky: >> Aside from that being how Python does it, why do you see that as >> preferable? > > Because: 1) linear searches in an array are damn common. I don't > remember the results of my benchmarks, but until your integer arrays > is quite longer than 30-50 items, performing a linear search is > faster than a lookup in an AA, on DMD. On Tango this number is > probably 70% higher 1b) In Python if you perform a "foo" in "barfoo" > the language doesn't perform a linear search, it uses a much smarter > search that has a complexity lower than the product of the two > lengths, using a custom algorithm. So in D you can use the same > syntax to search for substrings/subarrays. Where such smarter search > is not possible, D can use a naive search. 2) It's really handy. I > use isIn(item, items) to search on arrays in D, but having a item in > items is nicer. 3) You can use the same syntax to search into > anything that's lazily iterable too (a Range). This is very handy.
I would add to that: 4) Because 'in' is an operator, and operators are expected to bear a greater weight than ordinary functions. If 'in' was an ordinary method, say 'a.contains(b)', then I would choose different method names for searching an array for a value and searching an associative array for a key. Probably something like: array.contains(value) associative_array.containsKey(key) However, since 'in' already is an infix operator, it should have the most widely applicable semantics. Operators are heavyweight syntactic sugar for function calls. There is no room in D for operators that are only rarely useful. -- Rainer Deyke - rain...@eldwood.com