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.

#![feature(const_trait_impl)]

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;

type FType = fn(u8, u8, u8) -> u8;
type FXType = Curried<(u8,), (), &'static FType>;
type FXZType = Curried<(), (u8,), &'static FXType>;
type FXYZType = Curried<(u8,), (), &'static FXZType>;

const F: FType = f;
const FX: FXType = F.curry(X);
const FXZ: FXZType = FX.rcurry(Z);
const FXYZ: FXYZType = FXZ.curry(Y);

assert_eq!(FX(Y, Z), f(X, Y, Z));
assert_eq!(FXZ(Y), f(X, Y, Z));
assert_eq!(FXYZ(), f(X, Y, Z));

Structs§

Curried
A struct which represents a curried function.

Traits§

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.