#stream #fold #async

stream-reduce

Fold a stream without an initial value

1 unstable release

0.1.0 Apr 5, 2020

#1887 in Asynchronous

Download history 2184/week @ 2024-03-13 395/week @ 2024-03-20 446/week @ 2024-03-27 1335/week @ 2024-04-03 417/week @ 2024-04-10 784/week @ 2024-04-17 366/week @ 2024-04-24 946/week @ 2024-05-01 178/week @ 2024-05-08 475/week @ 2024-05-15 519/week @ 2024-05-22 467/week @ 2024-05-29 179/week @ 2024-06-05 263/week @ 2024-06-12 155/week @ 2024-06-19 119/week @ 2024-06-26

750 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
~16K SLoC