#stream #fold #async

stream-reduce

Fold a stream without an initial value

1 unstable release

0.1.0 Apr 5, 2020

#2118 in Asynchronous

Download history 81/week @ 2024-11-22 81/week @ 2024-11-29 305/week @ 2024-12-06 472/week @ 2024-12-13 87/week @ 2024-12-20 23/week @ 2024-12-27 122/week @ 2025-01-03 198/week @ 2025-01-10 113/week @ 2025-01-17 41/week @ 2025-01-24 336/week @ 2025-01-31 130/week @ 2025-02-07 156/week @ 2025-02-14 63/week @ 2025-02-21 117/week @ 2025-02-28 261/week @ 2025-03-07

610 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

~0.6–0.8MB
~15K SLoC