Skip to content

Instantly share code, notes, and snippets.

@HParker
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save HParker/9199843 to your computer and use it in GitHub Desktop.

Select an option

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.
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