#stream #fold #async

stream-reduce

Fold a stream without an initial value

1 unstable release

0.1.0 Apr 5, 2020

#1769 in Asynchronous

Download history 511/week @ 2024-07-19 765/week @ 2024-07-26 245/week @ 2024-08-02 150/week @ 2024-08-09 134/week @ 2024-08-16 250/week @ 2024-08-23 289/week @ 2024-08-30 152/week @ 2024-09-06 63/week @ 2024-09-13 176/week @ 2024-09-20 343/week @ 2024-09-27 380/week @ 2024-10-04 156/week @ 2024-10-11 165/week @ 2024-10-18 256/week @ 2024-10-25 342/week @ 2024-11-01

1,015 downloads per month

MIT license

6KB
101 lines

stream-reduce

reduce() for streams in Rust

Build Status Latest Version Rust Documentation

[dependencies]
stream-reduce = "0.1"

lib.rs:

This crate gives Streams a reduce function that is similar to fold but without an initial value. The function returns a Future containing None if the stream is empty and Some(value) otherwise.

Based on David Tolnay's reduce crate for iterators.

Examples

use stream_reduce::Reduce;
use futures::stream;

async {
    // Reduce a non-empty stream into Some(value)
    let v = vec![1usize, 2, 3, 4, 5];
    let sum = stream::iter(v).reduce(|a, b| async move { a + b }).await;
    assert_eq!(Some(15), sum);

    // Reduce an empty stream into None
    let v = Vec::<usize>::new();
    let product = stream::iter(v).reduce(|a, b| async move { a * b }).await;
    assert_eq!(None, product);
}

Dependencies

~1MB
~15K SLoC