Trait currycompose::Compose

source ·
pub trait Compose<F, XG, XF>: Sized {
    // Required method
    fn compose(self, with: F) -> Composition<Self, F, XG, XF>;
}
Expand description

https://en.wikipedia.org/wiki/Function_composition

Trait for composing two functions (currying and non-curring composition)

Non-currying composition: h(x) = g ∘ f = g(f(x))

Currying composition: h(…, x) = g ∘ f = g(f(x), …)

When currying, arguments of the function being curried with (f) is moved to the end of the argument-list

Both operands must implement FnOnce. If both implement FnMut or Fn, the resulting composition will also implement these traits.

g must also have one or more argument, where the first argument type equals the return type of f.

#![feature(generic_const_exprs)]
 
use currycompose::*;
 
// g ∘ f
// where
// g :: f32 -> f32
// f :: u8 -> f32
// g ∘ f :: u8 -> f32
let g = |x: f32| x*x;
let f = |x: u8| x as f32;
 
let gf = g.compose(f);
 
let x = 1;
 
assert_eq!(gf(x), g(f(x)));
 
// g ∘ f
// where
// g :: f32 -> f32 -> f32
// f :: u8 -> f32
// g ∘ f :: f32 -> u8 -> f32
let g = |x: f32, y: f32| x + y;
let f = gf;
 
let gf = g.compose(f);
 
let x = 1;
let y = 1.0;
 
// note here the argument x has been shifted to the end of the args in gf
assert_eq!(gf(y, x), g(f(x), y));
 
// g ∘ f ∘ f
// where
// g :: f32 -> f32 -> f32
// f :: u8 -> f32
// g ∘ f ∘ f :: u8 -> u8 -> f32
let gff = gf.compose(f);
 
let x = 1;
let y = 1;
 
assert_eq!(gff(x, y), g(f(x), f(y)));

Required Methods§

source

fn compose(self, with: F) -> Composition<Self, F, XG, XF>

Composing two functions

h(x) = g ∘ f = g(f(x))

Implementors§

source§

impl<G, F, XG, XF> Compose<F, XG, XF> for Gwhere XG: Tuple + TupleUnprepend<XG>, XF: Tuple, Self: FnOnce<XG>, F: FnOnce<XF, Output = Head<XG>>, (Tail<XG>, XF): TupleConcat<Tail<XG>, XF>, ConcatTuples<Tail<XG>, XF>: Tuple, Composition<Self, F, XG, XF>: FnOnce<ConcatTuples<Tail<XG>, XF>>,