map on fixed-size arrays

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Aug 21 05:13:12 PDT 2010


On 21.08.2010 14:37, Eduardo Cavazos wrote:
> Hello,
>
> The 'map' from std.algorithm doesn't seem to work with fixed-size arrays:
>
> ----------------------------------------------------------------------
> import std.stdio ;
> import std.math ;
> import std.algorithm ;
>
> T sq ( T ) ( T x ) { return x*x ; }
>
> void main ()
> {
>   double [2] a = [ 1.0 , 2.0 ] ;
>
>   writeln ( map ! ( sq ) ( a ) ) ;
> }
> ----------------------------------------------------------------------
>
> $ rdmd test_map_sq_fixed_size_b.d
> /usr/include/d/dmd/phobos/std/algorithm.d(108): Error: template 
> instance Map!(sq,double[2u]) does not match template declaration 
> Map(alias fun,Range) if (isInputRange!(Range))
>
> Is this an intended limitation?
>
> Ed
You always can workaround this by taking full slice:

import std.stdio ;
import std.math ;
import std.algorithm ;

T sq ( T ) ( T x ) { return x*x ; }

void main ()
{
   double [2] a = [ 1.0 , 2.0 ] ;

   writeln ( map ! ( sq ) ( a[] ) ) ;
}

I'm not sure if it's by design.

-- 
Dmitry Olshansky



More information about the Digitalmars-d mailing list