Skip to content

Instantly share code, notes, and snippets.

@davidjbeveridge
Created April 9, 2015 17:09
Show Gist options
  • Save davidjbeveridge/a4cc979b69c205ad1b4b to your computer and use it in GitHub Desktop.
Save davidjbeveridge/a4cc979b69c205ad1b4b to your computer and use it in GitHub Desktop.
Function composition in Ruby
class Lambda < Proc
def initialize(&block)
@block = block
end
def >> (g)
Lambda.new {|*args| g[*self[*args]]}
end
def method_missing(name, *args, &block)
@block.send name, *args, &block
end
end
def λ (&block)
Lambda.new &block
end
double = λ {|x| x*2}
addOne = λ {|x| x+1}
puts double.(5)
puts addOne[double[5]]
puts (double >> addOne)[5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment