boolean over multiple variables

bearophile bearophileHUGS at lycos.com
Tue Jan 26 13:21:57 PST 2010


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.


> So having a single syntax work on the outputs for 
> regular arrays, but then on the inputs for AAs, seems highly inconsistent 
> and error-prone to me.

I have followed many Python newbies personally, I am following the Python newsgroups, and I have programmed for years in Python, and while I have seen many different kinds of bugs, I have not seen a significant amount of bugs in this. Python programmers just learn that dicts and lists are a little different in this regard. At the same way they learn that a set and a dict are different data structures, with different capabilities and usages.

Why don't you start using Python, I think in 5 days you can tell that's easy to not confuse the following usages:
5 in {5:1, 2:2, 5:3}
5 in [1, 2, 5]
"5" in "125"
"25" in "125"

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list