Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created December 3, 2013 12:06
Show Gist options
  • Save jonelf/7768152 to your computer and use it in GitHub Desktop.
Save jonelf/7768152 to your computer and use it in GitHub Desktop.
# Recursive
def dot_product(xs, ys, acc = 0)
if xs.empty? or ys.empty?
acc
else
dot_product(xs.drop(1), ys.drop(1), acc + xs.first * ys.first)
end
end
# More idiomatic Ruby
def dot_product(xs, ys)
xs.zip(ys).map{|n,m| n * m}.reduce(:+)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment