Created
July 17, 2013 03:56
-
-
Save cjavdev/6017577 to your computer and use it in GitHub Desktop.
Ruby my_curry_sum demonstrating closures.
This file contains 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
def my_curry_sum x # x= number of times to curry | |
sum = 0 | |
# this will not work with a proc as a proc will return | |
# out of the my_curry_sum method | |
sum_prc = lambda do |n| | |
sum += n | |
x -= 1 | |
return sum if x == 0 | |
return sum_prc | |
end | |
return sum_prc | |
end | |
sum = my_curry_sum(3) | |
p sum[1][2][3] # this is a fancy way of doing sum.call(1).call(2).call(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment