[][src]Trait rxrust::ops::reduce::Reduce

pub trait Reduce<OutputItem> {
    fn reduce_initial<InputItem, BinaryOp>(
        self,
        initial: OutputItem,
        binary_op: BinaryOp
    ) -> ReduceOp<Self, BinaryOp, InputItem, OutputItem>
    where
        Self: Sized,
        BinaryOp: Fn(OutputItem, InputItem) -> OutputItem,
        OutputItem: Clone
, { ... }
fn reduce<InputItem, BinaryOp>(
        self,
        binary_op: BinaryOp
    ) -> LastOrOp<ScanOp<Self, BinaryOp, InputItem, OutputItem>, OutputItem>
    where
        Self: Sized,
        BinaryOp: Fn(OutputItem, InputItem) -> OutputItem,
        OutputItem: Default + Clone
, { ... } }

The Reduce operator applies a function to the first item emitted by the source observable and then feeds the result of the function back into the function along with the second item emitted by the source observable, continuing this process until the source observable emits its final item and completes, whereupon the observable returned from Reduce emits the final value returned from the function.

Provided methods

fn reduce_initial<InputItem, BinaryOp>(
    self,
    initial: OutputItem,
    binary_op: BinaryOp
) -> ReduceOp<Self, BinaryOp, InputItem, OutputItem> where
    Self: Sized,
    BinaryOp: Fn(OutputItem, InputItem) -> OutputItem,
    OutputItem: Clone

Apply a function to each item emitted by an observable, sequentially, and emit the final value, after source observable completes.

Emits error when source observable emits it.

Arguments

  • initial - An initial value to start the successive reduction from.
  • binary_op - A closure acting as a binary (folding) operator.

Examples

use rxrust::prelude::*;
use rxrust::ops::Reduce;

observable::from_iter(vec![1, 1, 1, 1, 1])
  .reduce_initial(100, |acc, v| acc + v)
  .subscribe(|v| println!("{}", v));

// print log:
// 105

fn reduce<InputItem, BinaryOp>(
    self,
    binary_op: BinaryOp
) -> LastOrOp<ScanOp<Self, BinaryOp, InputItem, OutputItem>, OutputItem> where
    Self: Sized,
    BinaryOp: Fn(OutputItem, InputItem) -> OutputItem,
    OutputItem: Default + Clone

Works like [reduce_initial] but starts with a value defined by a Default trait for the first argument f operator operates on.

Arguments

  • binary_op - A closure acting as a binary operator.
Loading content...

Implementors

impl<O, OutputItem> Reduce<OutputItem> for O[src]

Loading content...