pub fn fix<T, R, F>(init: T, closure: F) -> R
Expand description
Fixpoint combinator for rust closures, generalized over the return type.
This is a wrapper function that uses the Fix
type. The recursive closure
has two arguments, Fix
and the argument type T
.
In Fix<T, R>, T is the argument type, and R is the return type, R defaults to T.
Calling the Fix
value only supports function call notation with the nightly
channel and the crate feature ‘unstable’ enabled; use the .call() method otherwise.
This helper function makes the type inference work out well.
use odds::fix;
assert_eq!(120, fix(5, |f, x| if x == 0 { 1 } else { x * f.call(x - 1) }));
let data = [true, false];
assert!(!fix(&data[..], |f, x| {
x.len() == 0 || x[0] && f.call(&x[1..])
}));