Last active
January 20, 2016 19:30
-
-
Save ripesunflower/2aaf68a62be7ef727548 to your computer and use it in GitHub Desktop.
Calculating with Functions | Codewars
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
[:zero, :one, :two, :three, :four, :five, | |
:six, :seven, :eight, :nine].each.with_index do |method, index| | |
define_method method do |function = nil| | |
(function) ? function.call(index) : index | |
end | |
end | |
[[:plus, :+], [:minus, :-], | |
[:times, :*], [:divided_by, :/]].each do |(method, action)| | |
define_method method do |second_operand| | |
->(first_operand) { first_operand.send(action, second_operand) } | |
end | |
end | |
seven(times(five)) # => 35 | |
four(plus(nine)) # => 13 | |
eight(minus(three)) # => 5 | |
six(divided_by(two)) # => 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment