#iterator #iter #flow #declarative #functional

no-std iter-flow

Functional programming utilities for Rust

1 unstable release

0.1.0 Feb 2, 2023

#1552 in Rust patterns

Download history 26/week @ 2023-02-02 4/week @ 2023-02-09 3/week @ 2023-02-16 76/week @ 2023-03-16 6/week @ 2023-03-23 3/week @ 2023-03-30 11/week @ 2023-04-06 5/week @ 2023-04-13 3/week @ 2023-04-20 10/week @ 2023-04-27 44/week @ 2023-05-04 62/week @ 2023-05-11 52/week @ 2023-05-18

168 downloads per month
Used in intelli-shell

Apache-2.0

25KB
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:

# use iter_flow::Iterflow;
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