Sha256: 160bd7873f0d88059d0560a48a121f4df75e4c8637fdf642f3aa7bdf36362ca7

Contents?: true

Size: 965 Bytes

Versions: 4

Compression:

Stored size: 965 Bytes

Contents

class Array
  # Similar to +reduce+/+inject+, but also returns intermediate values. Has the 
  # same interface as +reduce+/+inject+, so an initial value, an operator or 
  # both can be supplied. This method may eventually be moved to the 
  # +Enumerable+ module.
  #
  # @example Symbol argument
  #   a = (1..4).to_a
  #   a.reductions(:+) #=> 10
  # @example Initial value and symbol argument
  #   a.reductions(50, :+) #=> 60
  # @example Block argument
  #   %w(a b c).reductions { |s1, s2| s1+s2 } #=> ["a", "ab", "abc"]
  def reductions(*args, &block)
    arr = dup

    if args.size == 1
      Symbol === (arg = args.first) ? op = arg : initial = arg
    else
      initial, op, _ = args
    end

    raise ArgumentError, "Need an operator or block" unless op || block

    initial ||= arr.shift

    arr.inject([initial, [initial]]) { |(acc, result), el|
      val = op ? acc.send(op, el) : yield(acc, el)
      [val, result << val]
    }.last
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shenanigans-1.0.17 lib/shenanigans/array/reductions.rb
shenanigans-1.0.16 lib/shenanigans/array/reductions.rb
shenanigans-1.0.15 lib/shenanigans/array/reductions.rb
shenanigans-1.0.14 lib/shenanigans/array/reductions.rb