Skip to content

Instantly share code, notes, and snippets.

@ivanacostarubio
Created October 2, 2012 15:08
Show Gist options
  • Select an option

  • Save ivanacostarubio/3819922 to your computer and use it in GitHub Desktop.

Select an option

Save ivanacostarubio/3819922 to your computer and use it in GitHub Desktop.
Higher Order Function in Ruby
#
# Intro to Higher Order Functions
#
# translated to Ruby from Martin Scala Coursera.org class
#
def cube(n)
n ** n
end
def square(n)
n ** 2
end
def sum_ints(a,b)
if a > b
return 0
else
return a + sum_ints(a + 1, b)
end
end
def sum_square(a,b)
if a > b
return 0
else
square(a) + sum_square(a + 1, b)
end
end
def sum_cubes(a,b)
if a > b
return 0
else
cube(a) + sum_cubes(a + 1, b)
end
end
@l_square = lambda { |x| x * 2 }
@l_cube = lambda { |x| x ** x }
@l_ident = lambda { |x| x }
def zomg_sum(f,a,b)
if a > b
return 0
else
f.call(a) + zomg_sum(f,a + 1, b)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment