Function rust2fun::combinator::if_else

source ·
pub fn if_else<A, B, P, T, F>(predicate: P, if_true: T, if_false: F, x: A) -> Bwhere
    P: FnOnce(&A) -> bool,
    T: FnOnce(A) -> B,
    F: FnOnce(A) -> B,
Expand description

This function allows for conditionals in composition chains. Unlike converge, which branches and merges, if_else chooses which function to run based on the predicate, and the other function is ignored.

Example

use rust2fun::prelude::*;

let is_even = |x: &i32| x & 1 == 0;
let actual = if_else(is_even, |x| x / 2, |x| (x + 1) / 2, 7);
assert_eq!(4, actual);