!in

bearophile bearophileHUGS at lycos.com
Wed Feb 17 12:20:04 PST 2010


Michel Fortin:

>You're suggesting that 'in' for arrays could search not only for elements but also for subarrays?<

I have suggested this probably two years ago or more. The main difference compared the Python situation is that D is statically typed, so the compiler can tell apart the case of searching of a single item from the case of searching a subsequence.

A similar case: in Python lists have the .append() and .extend() methods because Python is dynamically typed, so it can't tell apart the two cases. But the static type system of D allows you to just use ~= for both cases.


> The interesting part is that the syntax is very natural. The less 
> interesting part is that it's slower and slightly different from 'in' 
> in associative arrays. I'm on the fence.

I am for it, because the operation is the same, "in" is used to search inside a collection, then the collection uses the faster method it has to answer.
But D2 is now finalized, so this feature might need to wait after the phase of debugging and improvement of D2.

--------------

Andrei Alexandrescu:

>Generally I'd be hesitant to make it all too easy to do linear searches. Too much of that would encourage people to stick with linear searches because it's the path of least resistance.<

D programmers know the difference between linear search, hash search and search in an ordered search tree. They are O(n), O(1), and O(ln n). Do you want to use a different name for each of those searches (and maybe a (n log log n if you search with interpolated search, etc) just because their computational complexity is different, or do you want just a syntax that asks the collection to find something using the faster code it has?

Even in my Python programs I am well aware of the performance difference between a linear search in a list and an hash search in a set. If I know I will need to perform a search operation often, on in an important loop, I will use a set, otherwise using a list (that is an array) is often good enough. The syntax to search is the same so you can swap data structure in a moment, eventually.


>Searching a value in a literal should actually be allowed: x in [10, 20, 30, 0]<

This is very wrong. No special cases, please. If you don't want to support the linear search on all arrays, then please don't add this feature at all, I will keep using my isIn templated function (that digests AAs too, lazy iterables, etc too of course). I don't want more warts and special cases in D, thank you. As Python Zen says: "Special cases aren't special enough to break the rules." Such rules must be read and understood by D devs too, if you want to make a language that's not a mess as C++.

Bye,
bearophile



More information about the Digitalmars-d mailing list