'in' syntax for searching arrays

Janice Caron caron800 at googlemail.com
Sun Nov 4 00:20:26 PDT 2007


On 11/4/07, Janice Caron <caron800 at googlemail.com> wrote:
> My own thought is that looking for values in arrays should have the
> same syntax as strings. That is, for non-associative arrays:
>
> V[] a;
> int n = a.find(x);
>
> should return -1 if the value is not present in the array, or the
> index if it is.

In point of fact, if find() were defined for arrays of any kind,
instead of just char[] (or now, "const const char[]" (whatever that
means), then the string functions would almost be subsumed.

(I say almost, because the string functions search for a dchar, not a
char, and hence do UTF-8 decoding)

Of course I'd also like to see the std.string functions extended to
include wstrings and dstrings, but that's off-topic for this thread.


> So then, my second thought was, maybe it should be
>
> V[K] a;
> K[] n = a.findAll(x); // return an array of all keys whose value is x

My third thought is a function findEach() which returns a class which
overloads foreach, so you could do

V[K] a;
foreach (key; a.findEach(value)) {}

But again, all I'm talking about here are library functions, not
language extentions.



More information about the Digitalmars-d mailing list