Skip to content

Instantly share code, notes, and snippets.

View HParker's full-sized avatar
🪄

Adam Hess HParker

🪄
View GitHub Profile
@HParker
HParker / nbeers.rb
Last active August 29, 2015 13:56
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)})})}