Crate currying

Crate currying 

Source
Expand description

A crate for currying functions in rust

Arguments can be passed one at a time, yielding a new something implementing FnOnce (and possibly FnMut and Fn) which can be called with one less argument.

It also implements AsyncFnOnce, AsyncFnMut and AsyncFn if the feature async is enabled, since this is an experimental feature.

Curried arguments are then omitted when calling the curried function, as they have already been passed.

§Examples

use currying::*;

let f = |x, y, z| x + y + z;
let (x, y, z) = (1, 2, 3);

let fx = f.curry(x);

assert_eq!(fx(y, z), f(x, y, z));

let fxz = fx.rcurry(z);

assert_eq!(fxz(y), f(x, y, z));

let fxyz = fxz.curry(y);

assert_eq!(fxyz(), f(x, y, z));

Currying also works at compile-time.

use currying::*;

const fn f(x: u8, y: u8, z: u8) -> u8 {
    x + y + z
}

const X: u8 = 1;
const Y: u8 = 2;
const Z: u8 = 3;

const {
    let fx = f.curry(X);

    assert!(fx(Y, Z) == f(X, Y, Z));

    let fxz = fx.rcurry(Z);

    assert!(fxz(Y) == f(X, Y, Z));

    let fxyz = fxz.curry(Y);

    assert!(fxyz() == f(X, Y, Z));
}

Structs§

Curried
A struct which represents a curried function.

Traits§

ConcatArgs
Curry
A trait providing the method for currying from the left.
RCurry
A trait providing the method for currying from the right.

Trait Aliases§

Curriable
A trait for things which may be curried.
RCurriable
A trait for things which may be curried.