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

Reply via email to