Created
August 3, 2012 03:26
-
-
Save yuroyoro/3244060 to your computer and use it in GitHub Desktop.
なんで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
# Rubyで関数合成 | |
# | |
# irb(main):016:0> f = "foo".method(:==) | |
# => #<Method: String#==> | |
# irb(main):017:0> g = lambda{|x| puts "result : #{x}"} | |
# => #<Proc:0x007fdafa06c0c0@(irb):17 (lambda)> | |
# irb(main):018:0> (f >> g).call("foo") | |
# result : true | |
# => nil | |
# irb(main):019:0> (f >> :to_s).call("hoge") | |
# => "false" | |
module ComposableFunction | |
def >>(g) | |
lambda{|*args| g.to_proc.call(self.to_proc.call(*args)) } | |
end | |
def <<(g) | |
g.to_proc >> self | |
end | |
end | |
[Proc, Method, Symbol].each do |klass| | |
klass.send(:include, ComposableFunction) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment