Reduce Operator

Base.reduceFunction
reduce(::Type{R}, reduceFn::Function, seed::R) where R
reduce(reduceFn::F) where { F <: Function }

Creates a reduce operator, which applies a given accumulator reduceFn function over the source Observable, and returns the accumulated result when the source completes, given an optional seed value. If a seed value is specified, then that value will be used as the initial value for the accumulator. If no seed value is specified, the first item of the source is used as the seed.

Arguments

  • ::Type{R}: the type of data of transformed value
  • reduceFn::Function: transformation function with (data::T, current::R) -> R signature
  • seed::R: optional seed accumulation value

Producing

Stream of type <: Subscribable{R}

Examples

using Rocket

source = from([ i for i in 1:10 ])
subscribe!(source |> reduce(Vector{Int}, (d, c) -> [ c..., d ], Int[]), logger())
;

# output

[LogActor] Data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[LogActor] Completed
using Rocket

source = from([ i for i in 1:42 ])
subscribe!(source |> reduce(+), logger())
;

# output

[LogActor] Data: 903
[LogActor] Completed

See also: AbstractOperator, RightTypedOperator, ProxyObservable, logger

source

Description

reduce applies an accumulator function to each value of the source Observable (from the past) and reduces it to a single value that is emitted by the output Observable. Note that reduce will only emit one value, only when the source Observable completes. It is equivalent to applying scan followed by last.

It returns an Observable that applies a specified accumulator function to each item emitted by the source Observable. If a seed value is specified, then that value will be used as the initial value of the accumulator. If no seed value is specified, the first item of the source is used as the seed.

See also

Operators, scan, last