std.algorithm.find and array of needles?

John Colvin john.loughran.colvin at gmail.com
Mon Mar 3 12:58:51 PST 2014


On Monday, 3 March 2014 at 19:35:53 UTC, captaindet wrote:
> std.algorithm.find has an overload that works with several 
> needles:
>
> // phobos doc example:
> int[] a = [ 1, 4, 2, 3 ];
> assert(find(a, [ 1, 3 ], 4) == tuple([ 4, 2, 3 ], 2));
>
> the function i want to write has to deal with variadic 
> arguments serving as needles. unfortunately, i failed trying to 
> make find work with the needles provided in an array:
>
> int[] a = [ 9, 8, 7, 6, 5, 4, 3, 2, 1 ];
> int[][] ns = [  [ 7, 6 ], [ 4, 3 ] ];
> auto res = find( a, ns );
> //Error: template std.algorithm.find does not match any 
> function template declaration. Candidates are:
>
> any ideas how this can be achieved?
>
> thanks
> /det

You can use a variadic template function instead of variadic 
slice construction:

void foo(A, T ...)(A[] a, T ns)
{
     //use find as follows. For example:
     return a.find(ns);
}

assert(foo([1,2,3,4], 3, 6, 2) == Tuple!([2,3,4], 2));


More information about the Digitalmars-d-learn mailing list