#--
# Thanks dave!
#++
class Proc

  # Returns a new proc that is the functional
  # compostion of two procs, in order.
  #
  #  a = lambda { |x| x + 4 }
  #  b = lambda { |y| y / 2 }
  #
  #  a.compose(b).call(4)  #=> 6
  #  b.compase(a).call(4)  #=> 4
  #
  def compose(g)
    raise ArgumentError, "arity count mismatch" unless arity == g.arity
    proc{ |*a| self[ *g[*a] ] }
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCProc < Test::Unit::TestCase

    def test_compose
      a = lambda { |x| x + 4 }
      b = lambda { |y| y / 2 }
      assert_equal( 6, (a.compose(b)).call(4) )
      assert_equal( 4, (b.compose(a)).call(4) )
    end

  end

=end
