Method: RxRuby::Observable#reduce

Defined in:
lib/rx_ruby/operators/aggregates.rb

#reduce(*args, &block) ⇒ RxRuby::Observable Also known as: aggregate

Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified seed value is used as the initial accumulator value. For aggregation behavior with incremental intermediate results, see RxRuby::Observable.scan

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rx_ruby/operators/aggregates.rb', line 47

def reduce(*args, &block)
  # Argument parsing to support:
  # 1. (seed, Symbol) || (seed, &block)
  # 2. (Symbol) || (&block)
  if (args.length == 2 && args[1].is_a?(Symbol)) || (args.length == 1 && block_given?)
    scan(*args, &block).start_with(args[0]).final
  elsif (args.length == 1 && args[0].is_a?(Symbol)) || (args.length == 0 && block_given?)
    scan(*args, &block).final
  else
    raise ArgumentError.new 'Invalid arguments'
  end
end