Skip to content

pipe

Combines two functions into one that applies first and then second to its result: pipe(first, second)(x) is second(first(x)).

The same as compose with the arguments in reading order.

use helpers4::function::pipe;

Cargo feature function (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features function

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["function"] }
pub fn pipe<A, B, C>(first: impl Fn(A) -> B, second: impl Fn(B) -> C) -> impl Fn(A) -> C
ParameterTypeDescription
firstimpl Fn(A) -> BThe function applied first.
secondimpl Fn(B) -> CThe function applied to the result of first.

impl Fn(A) -> C — A function from the input of first to the output of second.

use helpers4::function::pipe;

let shout = pipe(|s: &str| s.to_uppercase(), |s: String| s + "!");
assert_eq!(shout("hello"), "HELLO!");

src/function/pipe.rs