Using reduce() with tuples created by zip()
bearophile
bearophileHUGS at lycos.com
Thu Oct 31 12:52:07 PDT 2013
Craig Dillabaugh:
> I know I could use a simple foreach loop with my zip command, or
> even std.numeric.euclidean( ... ), but I am trying to be cute
> here!
import std.stdio, std.algorithm, std.range, std.math,
std.functional;
alias sum(T) = curry!(reduce!q{a + b}, cast(T)0);
double euclidDistance(double[] xs, double[] ys)
in {
assert(xs.length == ys.length);
} body {
return zip(xs, ys)
.map!(xy => (xy[0] - xy[1]) ^^ 2)
.sum!double
.sqrt;
}
void main() {
auto xs = [0.0, 1.0, 0.1];
auto ys = [1.0, -1.0, 0.0];
euclidDistance(xs, ys).writeln;
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list