Solutions to the Calculating with Functions CodeWars kata.
Last active
February 19, 2016 06:58
-
-
Save johncarney/5c7b91f8749fc77186c8 to your computer and use it in GitHub Desktop.
Calculating with functions
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
| # My first take on it. Dumb, repetitive, and probably slow, but it gets the job done. | |
| def zero(*v) | |
| eval [ 0, *v ].join | |
| end | |
| def one(*v) | |
| eval [ 1.0, *v ].join | |
| end | |
| def two(*v) | |
| eval [ 2.0, *v ].join | |
| end | |
| def three(*v) | |
| eval [ 3.0, *v ].join | |
| end | |
| def four(*v) | |
| eval [ 4.0, *v ].join | |
| end | |
| def five(*v) | |
| eval [ 5.0, *v ].join | |
| end | |
| def six(*v) | |
| eval [ 6.0, *v ].join | |
| end | |
| def seven(*v) | |
| eval [ 7.0, *v ].join | |
| end | |
| def eight(*v) | |
| eval [ 8.0, *v ].join | |
| end | |
| def nine(*v) | |
| eval [ 9.0, *v ].join | |
| end | |
| def plus(v) | |
| [ "+", *v ] | |
| end | |
| def minus(v) | |
| [ "-", *v ] | |
| end | |
| def times(v) | |
| [ "*", *v ] | |
| end | |
| def divided_by(v) | |
| [ "/", *v ] | |
| end |
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
| # A second go in which I eliminate the repetition and try to get fancy with lambdas. | |
| def number_generator(number) | |
| ->(operation = [ :to_f ]) { number.send(*operation) } | |
| end | |
| %w[ zero one two three four five six seven eight nine ].each_with_index do |name, index| | |
| define_method(name, &number_generator(index)) | |
| end | |
| def operator_generator(operator) | |
| ->(rhs) { [ operator, *rhs ] } | |
| end | |
| %w[ plus + minus - times * divided_by / ].each_slice(2) do |name, operator| | |
| define_method(name, &operator_generator(operator)) | |
| end |
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
| # The lambda thing was kinda fun, but didn't solve the problem any better than a more | |
| # prosaic solution and was a lot harder to understand. | |
| %i[ zero one two three four five six seven eight nine ].each_with_index do |name, number| | |
| define_method(name) do |operation = [ :to_f ]| | |
| number.send(*operation) | |
| end | |
| end | |
| %i[ plus + minus - times * divided_by / ].each_slice(2) do |name, operator| | |
| define_method(name) do |rhs| | |
| [ operator, *rhs ] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment