Skip to content

Instantly share code, notes, and snippets.

@gogotanaka
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gogotanaka/b8c5688863d9c4774cd8 to your computer and use it in GitHub Desktop.
Save gogotanaka/b8c5688863d9c4774cd8 to your computer and use it in GitHub Desktop.
Zコンビネーター、Ruby

Zコンビネーター、Ruby

Z = ->(f) {
  ->(x) {
    f[ ->(y) { x[x][y] } ]
  }[
    ->(x) {
      f[ ->(y) { x[x][y] } ]
    }
  ]
}

class Proc
  def ==(other)
    RubyVM::InstructionSequence.of(self) == RubyVM::InstructionSequence.of(other) 
  end
end

s = ->(x) { ->(y) { ->(z) { x[z][y[z]] }}}
k = ->(x) { ->(y) { x }}

s[Z[s]] == Z[s]
#=> true

k[Z[k]] == Z[k]
#=> true

1000.times.all? do
  f = eval rand(1000).times.inject("s") { |f| f + %w|[s] [k]|.sample }
  f[Z[f]] == Z[f]
end
#=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment