# -*- encoding : utf-8 -*- module LambdaDriver::Composable if RUBY_VERSION < "2.6.0" # Returns new lambda which composed self and given function. # A composed proc called with args, executes `self.(g(*args)). # # f = lambda{|x| x.to_s } # g = lambda{|y| y.length } # h = f compose g # h.(:hoge) # => 4 # # This method is aliased as `<<`. # # f << g # => f.compose(g) # def compose(g) lambda{|*args| self.to_proc.call(g.to_proc.call(*args)) } end # g compose self def >>(g) g.to_proc << self end def self.included(klass) klass.send(:alias_method, :<<, :compose) end else # Returns new lambda which composed self and given function. # A composed proc called with args, executes `self.(g(*args)). # # f = lambda{|x| x.to_s } # g = lambda{|y| y.length } # h = f compose g # h.(:hoge) # => 4 # # This method is aliased as `<<`. # # f << g # => f.compose(g) # def compose(g) self.to_proc << g.to_proc end # g compose self def >>(g) g.to_proc << self end def self.included(klass) klass.send(:alias_method, :<<, :compose) end end end