1 unstable release

0.1.0 Feb 2, 2023

#2531 in Rust patterns

Download history 8/week @ 2024-02-12 14/week @ 2024-02-19 22/week @ 2024-02-26 1/week @ 2024-03-04 6/week @ 2024-03-11 7/week @ 2024-03-18 17/week @ 2024-03-25 35/week @ 2024-04-01 21/week @ 2024-04-15

75 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