Created
November 10, 2012 17:32
-
-
Save zaphod42/4051853 to your computer and use it in GitHub Desktop.
Solution to the coin changer kata
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
| class Object | |
| def when_defined(primary, alternate) | |
| primary.call(self) | |
| end | |
| end | |
| class NilClass | |
| def when_defined(primary, alternate) | |
| alternate.call(self) | |
| end | |
| end | |
| module Bank | |
| module_function | |
| COINS = [25, 10, 5, 1] | |
| def change(amount) | |
| COINS. | |
| find { |coin| amount >= coin }. | |
| when_defined ->(coin) { [coin] + change(amount - coin) }, | |
| ->(coin) { [] } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment