Sha256: fcf708919773f6f5109f67cede775382877f08dbde4aa2f015b1815c3ea3c00b

Contents?: true

Size: 647 Bytes

Versions: 1

Compression:

Stored size: 647 Bytes

Contents

module TeguGears #:nodoc:
  
  module InstanceMethods
    
    # This allows me to compose two classes using TeguGears.
    def compose(f)
      lambda {|*args| f.call(self.call(*args))}
    end
    alias :| :compose
  end
end

class Object
  # Composes any number of functions.  The first argument is the input.
  # Use Struct if the argument list needs to be more expansive. 
  def compose(*args)
    param = args.shift
    args.inject {|composition, f| make_binomial_composition(composition, f)}.call(param)
  end
  
  def make_binomial_composition(f, g)
    lambda{|*args| g.call(f.call(*args))}
  end
  private :make_binomial_composition
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
davidrichards-tegu_gears-0.0.3 lib/tegu_gears/compose.rb