boolean over multiple variables
Bill Baxter
wbaxter at gmail.com
Tue Jan 26 13:42:32 PST 2010
On Tue, Jan 26, 2010 at 1:21 PM, bearophile <bearophileHUGS at lycos.com> 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.
>
>
>> 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.
It's not even really inconsistent if you just think about these data
structures in terms of function rather than form.
An array is often used as a simple set of things. "O in Array" means
"is O in that set of things"
An AA is a set of things that also have some associated data. "O in
AA" means "is O in that set of things" (not the ancillary data)
If you have an actual "set" data structure for containing a set of of
things, then "O in Set" means, again, "is O in that set of things".
(In fact the closest thing D has to a built-in set type is an AA with
"don't care" associated data, reinforcing the notion of AA as a set
plus extra data.)
--bb
More information about the Digitalmars-d-learn
mailing list