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 |
#1174 in Math
Used in 2 crates
(via polynomial_ops)
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
~38K SLoC