Last active
August 29, 2015 13:56
-
-
Save HParker/9199843 to your computer and use it in GitHub Desktop.
n bottles of beer on the wall in ruby using only anonymous 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
| puts ->() { # print out the result | |
| script = ->(b) { # accessory funciton to beer improver to improve readability. | |
| puts "#{b} bottles of beer on the wall. #{b} bottles of beer." | |
| puts "you take one down, pass it around, #{b-1} bottles of beer on the wall." | |
| } | |
| beer_improver = ->(partial) { # a function that does any one bottle of beer on the wall. | |
| ->(n) { n.zero? ? "no more bottles" : ->(b) {script.(b); return partial.(b)}.(n-1)}} | |
| y = ->(improver) { # Church's Y-Combinator. | |
| ->(gen) { gen.(gen) }.(->(gen) {beer_improver.(->(v) {gen.(gen).(v)})})} | |
| y.(beer_improver).(100) # how many beers. | |
| }.() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment