Skip to content

Instantly share code, notes, and snippets.

@dLobatog
Created June 24, 2013 21:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dLobatog/5853677 to your computer and use it in GitHub Desktop.
Save dLobatog/5853677 to your computer and use it in GitHub Desktop.
sum = lambda do |f,a,b|
s = 0; a.upto(b){|n| s += f.(n) } ; s
end
#Square all numbers from a to b and sum them.
sum.(lambda{|x| x**2},1,5) #=> 55
#This is probably the above in idiomatic Ruby
(1..5).map { |x| x**2 }.reduce(:+) #=> 55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment