Skip to content

Instantly share code, notes, and snippets.

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

  • Save cronin101/9181036 to your computer and use it in GitHub Desktop.

Select an option

Save cronin101/9181036 to your computer and use it in GitHub Desktop.
Y Combinator example in Ruby
def apply_lots(function: ->(x) { x }, times: 1, initial: nil)
y_combinator = ->(f) {
->(x) { x.(x) }.(
->(x) { f.(->(*v) { x.(x).(*v) }) }
)
}
times_step = ->(partial_fn) {
->(fn, rem, acc) { rem.zero? ? acc : partial_fn.(fn, rem - 1, fn.(acc)) }
}
y_combinator.(times_step).(function, times, initial)
end
apply_lots function: ->(x) { x * x }, times: 4, initial: 2
# => 65536
apply_lots function: ->(_) { puts "derp" }, times: 10
# => nil
# derp
# derp
# derp
# derp
# derp
# derp
# derp
# derp
# derp
# derp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment