Skip to content

Instantly share code, notes, and snippets.

@burlesona
Created October 10, 2017 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burlesona/e54e385963f38b4fceffb4568c45b8d0 to your computer and use it in GitHub Desktop.
Save burlesona/e54e385963f38b4fceffb4568c45b8d0 to your computer and use it in GitHub Desktop.
Functional Ruby
# 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