1 unstable release

0.1.0 Feb 2, 2023

#2414 in Rust patterns

Download history 7/week @ 2024-07-20 8/week @ 2024-07-27 13/week @ 2024-08-03 16/week @ 2024-08-10 7/week @ 2024-08-17 1/week @ 2024-08-31 1/week @ 2024-09-07 3/week @ 2024-09-14 18/week @ 2024-09-21 18/week @ 2024-09-28 11/week @ 2024-10-05 6/week @ 2024-10-12 15/week @ 2024-10-19 6/week @ 2024-10-26 43/week @ 2024-11-02

71 downloads per month
Used in 2 crates

Apache-2.0

26KB
586 lines

Iterflow

Functional programming utilities for Rust.

This crate is heavily inspired by itertools but with the main focus of chaining operations to develop in a declarative approach (as opposed to usual imperative)

fn sub_1(n: u32) -> Result<u32, &'static str> {
    if n == 0 { Err("illegal!") } else { Ok(n - 1) }
}

fn math_calc(n: u32) -> u32 {
    (n + 1) * 2
}

fn to_range(n: u32) -> Range<u32> {
    0..n
}

let it = (0..4)
    .map(sub_1)
    .and_then(sub_1)
    .map_ok(math_calc)
    .flat_map_ok(to_range);

iter_flow::assert_equal(
    it,
    vec![
        Err("illegal!"),
        Err("illegal!"),
        Ok(0),
        Ok(1),
        Ok(0),
        Ok(1),
        Ok(2),
        Ok(3),
    ],
);

TODO:

  • Support for async/await with Futures

License

Iterflow is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.


lib.rs:

Extra iterator methods.

To extend Iterator with methods in this crate, import the Iterflow trait:

use iter_flow::Iterflow;

Now, new methods like and_then are available on all Result's iterators:

let it = (0..4)
    .map(|n| if n == 0 { Err("invalid!") } else { Ok(n - 1) })
    .and_then(|n| if n > 0 { Ok(n - 1) } else { Err("illegal!") })
    .map_ok(|n| (n + 1) * 2)
    .flat_map_ok(|n| (0..n));

iter_flow::assert_equal(
    it,
    vec![
        Err("invalid!"),
        Err("illegal!"),
        Ok(0),
        Ok(1),
        Ok(0),
        Ok(1),
        Ok(2),
        Ok(3),
    ],
);

No runtime deps