#iterator #fold #initial #value #fn

deprecated 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

#17 in #fold

Download history 143/week @ 2024-07-12 115/week @ 2024-07-19 153/week @ 2024-07-26 324/week @ 2024-08-02 219/week @ 2024-08-09 158/week @ 2024-08-16 772/week @ 2024-08-23 385/week @ 2024-08-30 135/week @ 2024-09-06 90/week @ 2024-09-13 124/week @ 2024-09-20 139/week @ 2024-09-27 226/week @ 2024-10-04 92/week @ 2024-10-11 94/week @ 2024-10-18 52/week @ 2024-10-25

493 downloads per month
Used in 8 crates (6 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