!in
Michel Fortin
michel.fortin at michelf.com
Wed Feb 17 08:01:57 PST 2010
On 2010-02-17 10:22:01 -0500, bearophile <bearophileHUGS at lycos.com> said:
> I've seen this change:
> http://dsource.org/projects/dmd/changeset/388
>
> !in is one of the things I have asked in the first list of requests I
> have posted in this newsgroup a lot of time ago :-) Glad to see few of
> those things get implemented. Thank you.
Indeed, that's great.
> The related thing I was asking for, is to use "in" for a linear search
> of single items inside an array, as in Python, a very handy thing
> that's used frequently (CPython it also implements an efficient
> algorithm to search for a substring:
> http://effbot.org/zone/stringlib.htm But probably Andrei wants such
> substring search to be a separated algorithm, even if it's very
> specifically tuned for strings only and not for arrays in general. I
> can understand this).
Are you sure the 'not in' operator in the Python algorithm you liked to
works like D's '!in'? It seems to me like it's searching for the
absence of a substring. In D 'in' and '!in' only look for one element.
I agree that it'd be handy to have 'in' and '!in' work with arrays,
especially with array literals:
if (x in [1,2,5,6,10])
do(something);
Perhaps this syntax could be allowed but only for array literals. The
reason being that array literals are usually short so a linear search
wouldn't be too bad. Moreover, an array literal being a literal, it's
easy for the compiler to optimize things; you're not constrained to a
linear search.
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list