Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created August 3, 2012 03:26
Show Gist options
  • Save yuroyoro/3244060 to your computer and use it in GitHub Desktop.
Save yuroyoro/3244060 to your computer and use it in GitHub Desktop.
なんでRubyで関数合成できねぇンだよ( ;゚皿゚)ノシΣ フィンギィィーーッ!!!
# 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