#iterator #fold #value #initial #fn

reduce

Fold an iterator without an initial value

6 releases

0.1.5+deprecated Dec 19, 2022
0.1.4 Dec 27, 2020
0.1.3 Jun 16, 2020
0.1.2 May 21, 2019
0.1.1 Jun 19, 2016

#16 in #fold

Download history 342/week @ 2023-11-26 169/week @ 2023-12-03 423/week @ 2023-12-10 370/week @ 2023-12-17 123/week @ 2023-12-24 218/week @ 2023-12-31 455/week @ 2024-01-07 220/week @ 2024-01-14 105/week @ 2024-01-21 43/week @ 2024-01-28 91/week @ 2024-02-04 143/week @ 2024-02-11 108/week @ 2024-02-18 201/week @ 2024-02-25 177/week @ 2024-03-03 53/week @ 2024-03-10

572 downloads per month
Used in 9 crates (7 directly)

MIT/Apache

8KB

iter.reduce(fn)

github crates.io docs.rs build status

This crate gives Iterators a reduce function that is similar to fold but without an initial value. The function returns None if the iterator is empty and Some(value) otherwise. This matches the distinction between reduce and fold in Scala.

[dependencies]
reduce = "0.1"

Examples

use reduce::Reduce;

fn main() {
    // Reduce a non-empty iterator into Some(value)
    let v = vec![1usize, 2, 3, 4, 5];
    let sum = v.into_iter().reduce(|a, b| a + b);
    assert_eq!(Some(15), sum);

    // Reduce an empty iterator into None
    let v = Vec::<usize>::new();
    let sum = v.into_iter().reduce(|a, b| a + b);
    assert_eq!(None, sum);
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps