Created
October 10, 2017 18:15
-
-
Save burlesona/e54e385963f38b4fceffb4568c45b8d0 to your computer and use it in GitHub Desktop.
Functional 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
# Not that this is an impressive demo, but it makes the point that Ruby | |
# does in fact have the ability to create "pure" functions and pass them | |
# as arguments to higher order functions. People get hung up on the syntax | |
# but it's not really that complicated. | |
make_adder = method def make_adder(num) | |
->(input){ input + num } | |
end | |
make_add_twice = method def make_add_twice(adder, num) | |
->{ adder[num] + adder[num] } | |
end | |
result_getter = method def result_getter(function) | |
function.call | |
end | |
add1 = make_adder(1) | |
four = make_add_twice(add1, 1) | |
puts "four!" if result_getter(four) == 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment