Sha256: 2a13429490a727e0479a32f30459b572b0a0413420d7a8d73404f19f826ebca0

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

# -*- 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lambda_driver-1.3.0 lib/lambda_driver/composable.rb