Created
October 2, 2012 15:08
-
-
Save ivanacostarubio/3819922 to your computer and use it in GitHub Desktop.
Higher Order Function in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # 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