Skip to content

compose

Combines two functions into one that applies inner first and outer to its result: compose(outer, inner)(x) is outer(inner(x)), like the mathematical outer ∘ inner.

See pipe for the same thing written in reading order.

use helpers4::function::compose;

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 compose<A, B, C>(outer: impl Fn(B) -> C, inner: impl Fn(A) -> B) -> impl Fn(A) -> C
ParameterTypeDescription
outerimpl Fn(B) -> CThe function applied last.
innerimpl Fn(A) -> BThe function applied first.

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

use helpers4::function::compose;

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

src/function/compose.rs