Home : 13 December 2011

avatar

Euclidean Distance in Ruby

Short and sweet euclidean distance lambda in Ruby (1.9+).

euclidean_distance = -> (p1, p2) do
  Math.sqrt(p1.zip(p2).map{ |a, b| a - b }.map{ |d| d * d }.reduce(:+))
end