Thread overview
How to search for an element in the array. D2 phobos.
Jul 12, 2009
Eldar Insafutdinov
Jul 12, 2009
bearophile
Jul 13, 2009
Bill Baxter
Jul 13, 2009
Daniel Keep
July 12, 2009
I am a little bit confused with a simple operation of searching in the array with D2-phobos. With tango I used to use:
    size_t find( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init );

    Performs a linear scan of buf from [0 .. buf.length), returning the index of the first element matching pat, or buf.length if no match was found. Comparisons will be performed using the supplied predicate or '==' if none is supplied.

I tried std.algorithm.find:

class Node { ... }

Node[] v;
Node node;
...
int pos = find(v, node);

I get this error: /home/eldar/d/bin/../src/phobos/std/algorithm.d(1617): Error: no property 'empty' for type 'model_d2.Node'

I think I completely misunderstood how to use it.

Many thanks in advance,
Eldar.
July 12, 2009
Eldar Insafutdinov:
> I think I completely misunderstood how to use it.

Yes, it's too much complex. It tries to do many different things in the most efficient way possible, the result is a high complexity in usage. That's one of the faults of Andrei's code, it's not tested by letting  average coders use it.
A solution for this problem is to offer simple functions (like you can find in Tango. In my dlibs the situation is intermediate, I think) with a simple API for the most common purposes.

Bye,
bearophile
July 13, 2009
On Sun, Jul 12, 2009 at 3:56 PM, bearophile<bearophileHUGS@lycos.com> wrote:
> Eldar Insafutdinov:
>> I think I completely misunderstood how to use it.
>
> Yes, it's too much complex. It tries to do many different things in the most efficient way possible, the result is a high complexity in usage. That's one of the faults of Andrei's code, it's not tested by letting  average coders use it.
> A solution for this problem is to offer simple functions (like you can find in Tango. In my dlibs the situation is intermediate, I think) with a simple API for the most common purposes.

Isn't that a matter of built-in arrays not yet sporting the range interface?
I assume if .empty is part of the range interface then arrays are
supposed to support it.

--bb
July 13, 2009

bearophile wrote:
> Eldar Insafutdinov:
>> I think I completely misunderstood how to use it.
> 
> Yes, it's too much complex. It tries to do many different things in the most efficient way possible, the result is a high complexity in usage. That's one of the faults of Andrei's code, it's not tested by letting  average coders use it.
> A solution for this problem is to offer simple functions (like you can find in Tango. In my dlibs the situation is intermediate, I think) with a simple API for the most common purposes.
> 
> Bye,
> bearophile

It should work.  Taken from the algorithm docs:

> Example:
>
> > int[] a = [ 1, 4, 2, 3 ];
> > assert(find(a, 4) == [ 4, 2, 3 ]);

That said, find is too complex for his purposes.  Amazingly, Phobos still appears to lack a basic indexOf function.

Phobos2: can build a working sentient AI that can feel love up from individual atoms, but can't find the index of an array element.  :P