In expression for array

bearophile bearophileHUGS at lycos.com
Thu Jan 24 00:44:27 PST 2008


>> How about adding In expressions for general arrays in addition to assoc arrays:
>>  if ('i' in eyes)

I like that, and I've said in the past.


> Not as it is presented there; I may have used that pattern twice in my 
> whole programming career (If order doesn't matter, a hashtable is a 
> better option).

For a programmer coming from Python that pattern is used all the time (even if Python has better syntaxes to define AAs). And if you look for example:
'l' in "hello"
you can see that a linear scan is actually faster than a hash if the number of items is small (< ~10) (and the opApply is fast enough).
That same syntax can be used to see if a substring is present in a string, and you can't do that with an hash...
In my d libs there are isIn()/isInSub() functions that allow to search of an element in linear structure (arrays, AA keys, iterable objects), or to search for a subsequence. I think Tango has similar functions among its algorithms.

Bye,
bearophile



More information about the Digitalmars-d mailing list