Created
April 9, 2015 17:09
-
-
Save davidjbeveridge/a4cc979b69c205ad1b4b to your computer and use it in GitHub Desktop.
Function composition in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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