vectorgraphics / asymptote

2D & 3D TeX-Aware Vector Graphics Language
https://asymptote.sourceforge.io/
GNU General Public License v3.0
556 stars 93 forks source link

An error with T2[] map(T2 f(T1), T1[] a) !? #251

Closed trongtat9 closed 3 years ago

trongtat9 commented 3 years ago
pair f(int x){ return (x,x+2);}
int[] a={0,2,4};
pair[] A=map(f,a);
write(A);

path g(pair x){ return x--x+(1,1);}
pair[] b={(1,0),(2,0),(3,0)};
path[] B=map(g,b);
write(B);

I get the error no matching function 'map(pair(int x), int[])'.

zzdatura commented 3 years ago

As per Section 6.12 in the User's Guide, the functions T2[] map(T2 f(T1), T1[] a) are required to be constructed explicitly by calling mapArray("T1", "T2"). So in your case you'd have to write:

mapArray("int", "pair");
pair f(int x){ return (x,x+2);}
int[] a={0,2,4};
pair[] A=map(f,a);
write(A);

mapArray("pair", "path");
path g(pair x){ return x--x+(1,1);}
pair[] b={(1,0),(2,0),(3,0)};
path[] B=map(g,b);
write(B);

Note that if you declare mapArray(...) in a module, the actual map functions will only be available after importing, therefore, the module itself cannot use the map functions:

module.asy:

...
mapArray("pair", "path");
...
map(g, b);    // <-- error

main.asy

import module;
...
map(g, b);   // <-- OK