Sha256: 9b980c5c69fa8c738dff0b9961c607f72872d74a6f4e5f704a52269829911a86

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

unless Enumerable.method_defined?(:sum)
  module Enumerable
    # Calculates a sum from the elements. Examples:
    #
    #  payments.sum { |p| p.price * p.tax_rate }
    #  payments.sum(&:price)
    #
    # The latter is a shortcut for:
    #
    #  payments.inject(0) { |sum, p| sum + p.price }
    #
    # It can also calculate the sum without the use of a block.
    #
    #  [5, 15, 10].sum # => 30
    #  ["foo", "bar"].sum # => "foobar"
    #  [[1, 2], [3, 1, 5]].sum => [1, 2, 3, 1, 5]
    #
    # The default sum of an empty list is zero. You can override this default:
    #
    #  [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
    #
    def sum(identity = 0, &block)
      if block_given?
        map(&block).sum(identity)
      else
        inject{|sum, element| sum + element } || identity
      end
    end
  end

  class Range #:nodoc:
    # Optimize range sum to use arithmetic progression if a block is not given and
    # we have a range of numeric values.
    def sum(identity = 0)
      return super if block_given? || !(first.instance_of?(Integer) && last.instance_of?(Integer))
      actual_last = exclude_end? ? (last - 1) : last
      (actual_last - first + 1) * (actual_last + first) / 2
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gorillib-0.6.0 lib/gorillib/enumerable/sum.rb
gorillib-0.5.2 lib/gorillib/enumerable/sum.rb
gorillib-0.5.0 lib/gorillib/enumerable/sum.rb
gorillib-0.4.2 lib/gorillib/enumerable/sum.rb
gorillib-0.4.2pre lib/gorillib/enumerable/sum.rb