#stream #fold #async

stream-reduce

无初始值地折叠流

1 个不稳定版本

0.1.0 2020年4月5日

#1760异步

Download history 1234/week @ 2023-12-07 905/week @ 2023-12-14 360/week @ 2023-12-21 137/week @ 2023-12-28 389/week @ 2024-01-04 643/week @ 2024-01-11 418/week @ 2024-01-18 247/week @ 2024-01-25 423/week @ 2024-02-01 279/week @ 2024-02-08 385/week @ 2024-02-15 195/week @ 2024-02-22 422/week @ 2024-02-29 1152/week @ 2024-03-07 1557/week @ 2024-03-14 380/week @ 2024-03-21

3,568 每月下载量

MIT 许可证

6KB
101

stream-reduce

Rust中流的reduce函数

Build Status Latest Version Rust Documentation

[dependencies]
stream-reduce = "0.1"

lib.rs:

此crate为Streams提供了一个类似foldreduce函数,但没有初始值。如果流为空,函数返回包含NoneFuture,否则返回Some(value)

基于David Tolnay的reduce crate for iterators。

示例

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);
}

依赖项

~1MB
~16K SLoC