#function #fp #curry #haskell

nightly currying

A crate for currying anything implementing FnOnce. 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.

4 releases

0.2.2 Nov 23, 2023
0.2.1 Jul 3, 2023
0.2.0 Jul 3, 2023
0.1.0 Mar 20, 2023

#731 in Rust patterns

Download history 16/week @ 2023-08-09 25/week @ 2023-08-16 12/week @ 2023-08-23 8/week @ 2023-08-30 13/week @ 2023-09-06 49/week @ 2023-09-13 44/week @ 2023-09-20 60/week @ 2023-09-27 19/week @ 2023-10-04 14/week @ 2023-10-11 15/week @ 2023-10-18 25/week @ 2023-10-25 13/week @ 2023-11-01 16/week @ 2023-11-08 24/week @ 2023-11-15 89/week @ 2023-11-22

150 downloads per month
Used in 3 crates (via polynomial_ops)

MIT license

10KB
153 lines

A crate for currying anything implementing FnOnce.

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.


lib.rs:

A crate for currying functions in rust

Currying is a functional programming term which essentially means to pass the first argument to a function, yielding a new function needing only the next following arguments.

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 fxy = fx.curry(y);

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

let fxyz = fxy.curry(z);

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;

const ASSERTIONS: [bool; 3] = {
    let fx = f.curry(X);
    let fxy = fx.curry(Y);
    let fxyz = fxy.curry(Z);
    [
        fx(Y, Z) == f(X, Y, Z),
        fxy(Z) == f(X, Y, Z),
        fxyz() == f(X, Y, Z)
    ]
};

assert_eq!(ASSERTIONS, [true; 3]);

Dependencies

~1.5MB
~32K SLoC