I would love having functions "map()" and "reduce()" that take user-defined functions. I would suggest that the other reduce functions like sum may be defined as functions that strictly operate on vectors. It would also be nice to be able to apply them as functions with the vector as an argument, rather than using a dot / postfix / member access style. Both have their merits. The weighted sum would become:
weight = [a, b, c, ... n];
y = xn.map(x, n -> x * weight[n]).reduce(x, sum : sum += x);
// or
y = sum( xn.map(x, n -> x * weight[n]) );
// or maybe
y = sum( map(xn, (x, n) -> {x * weight[n]}) );
And I think using explicit map and reduce would ease the ambiguity regarding whether xn.function() or function(xn) should apply to each input or the vector of inputs. E.g. xn.norm() is ambiguous, since it depends on what the elements of xn are. If xn's elements are all scalar values, then it should treat them as one vector and return the norm. But what if one of the inputs is a vector? Should it output a vector whose elements are the norm of each input? What if all the inputs are vectors? Contrast, the following are all totally unambiguous (right?):
norm(xn); xn.norm(); // return the norm of the vector whose elements are the inputs. Syntax error if any xn elements are vectors
xn.map(x -> norm(x)).reduce(x, sum: sum += x);
xn.reduce(x, sum -> sum += norm(x));
sum( xn.map(x -> norm(x)) );
Consequently, it appears that functions like norm() and sum() should only work on vectors, and xn or x.instances() should only be allowed to be treated as a vector if all xn or instances can be treated as scalar values? Otherwise you should probably explicitly map/reduce them, especially for a list of inputs with heterogeneous types.
I have few strong feelings about how to specify the anonymous function arguments. Definitely not C++ style. Python and Javascript are fine. There's also "|" enclosed arguments as seen in rust or supercollider:
x.instances().map(|x, n| x * weight[n]).sum();
I guess my personal preference is for something resembling an arrow.
All of this begs the question as well: if there can be a norm() function or a normalize() function, how about squared_norm()? How about linear_map(x, input_min, input_max, output_min, output_max)? Maybe a bit off topic, but I've always wondered whether it would be convenient to have such helper functions rather than having to always do the math. Webmapper helps a lot in that regard, but it might still be useful e.g. if you're making maps directly in C.
TW