lib/lambda_driver/composable.rb in lambda_driver-1.2.4 vs lib/lambda_driver/composable.rb in lambda_driver-1.3.0
- old
+ new
@@ -1,27 +1,56 @@
# -*- encoding : utf-8 -*-
module LambdaDriver::Composable
- # Returns new lambda which composed self and given function.
- # A composed proc called with args, executes `self.(g(*args)).
- #
- # f = lamdba{|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
+ 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
+ # g compose self
+ def >>(g)
+ g.to_proc << self
+ end
- def self.included(klass)
- klass.send(:alias_method, :<<, :compose)
+ 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