Issue 1323

Jonathan M Davis jmdavisProg at gmx.com
Sun Jan 9 15:31:30 PST 2011


On Sunday 09 January 2011 10:44:39 Andrei Alexandrescu wrote:
> On 1/9/11 10:28 AM, Pelle wrote:
> > On 01/09/2011 03:07 AM, Robert Clipsham wrote:
> >> On 08/01/11 23:20, bearophile wrote:
> >>> Andrei has recently closed issue 1323, it's a small but very useful
> >>> feature, so I suggest some public discussion:
> >>> http://d.puremagic.com/issues/show_bug.cgi?id=1323
> >>> 
> >>> Lines like this is present thousands of time in my Python code:
> >>> n in [1, 2, 3]
> >>> c in "hello"
> >>> "llo" in some_string
> >>> 
> >>> Bye,
> >>> bearophile
> >> 
> >> I'd be all for this, except it's inconsistent.
> >> ----
> >> auto arr = [ "foo" : 1, "bar" : 2 ];
> >> assert("foo" in arr);
> >> ----
> >> in for associative arrays works by key, if it works by value for normal
> >> arrays it's inconsistent, and if it works for keys people will wonder
> >> why it's not working as expected.
> > 
> > No, people will not wonder about that. See also: python. Seriously
> > people, stop giving this as a reason.
> > 
> > A much better argument is the in operator should run in O(log n) for
> > anything, meaning it won't work for a general unsorted array.
> > 
> > I suggest going for (v in assumeSorted(arr)) or (arr.contains(v)), where
> > contains is the canFind function blessed with a new, superior name. :-)
> 
> I'd be glad to change canFind to contains. Vote by replying to this. We
> can put canFind on the slow deprecation chute.
> 
> Andrei

I would remind you of http://d.puremagic.com/issues/show_bug.cgi?id=4405 where 
we considered replacing canFind() with any(). I think that contains() is exactly 
what we should have for the typical case of dealing with finding a specific 
element in an array or container, but we probably should have any() for the 
general case where you're looking to see whether the given predicate is true for 
_any_ element in the range. Adding all() to go along with it (as in it returns 
true if the given predicate is true for _all_ elements in the range) would be 
preferable.

However, I definitely think that having contains() makes a _lot_ of sense, 
because that is exactly the function name that most people are looking for when 
they want to know whether a particular element is in a range.

- Jonathan M Davis


More information about the Digitalmars-d mailing list