www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - map on fixed-size arrays

reply Eduardo Cavazos <wayo.cavazos gmail.com> writes:
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
Aug 21 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Eduardo Cavazos:

 The 'map' from std.algorithm doesn't seem to work with fixed-size arrays:

See bug 4114 In my opinion map, sort, etc have to work with fixed-sized arrays too (otherwise I'll have to write more wrappers). Bye, bearophile
Aug 21 2010
prev sibling next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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

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
Aug 21 2010
prev sibling next sibling parent Pelle <pelle.mansson gmail.com> writes:
On 08/21/2010 12:37 PM, 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

IIRC, it's intended. Use a[] to get a dynamic array from a.
Aug 21 2010
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 8/21/10 5:37 CDT, 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

To some extent, yes; fixed-size arrays are passed by value and most of the time you don't want that with an algorithm. You have the burden to append "[]" to fixed-size arrays so they are passed inside the algorithms as dynamic-length slices. std.algorithm could detect that and take care of that detail for you, at the cost of duplicating most function signatures. Andrei
Aug 21 2010