[][src]Crate fix_fn

This library enables the creation of recursive closures by providing a single macro fix_fn. The functionality is similar to the Y combinator. Recursive closures can have arbitrary amounts of parameters and can capture variables.

use fix_fn::fix_fn;

let fib = fix_fn!(|fib, i: u32| -> u32 {
    if i <= 1 {
     } else {
         // fib will call the closure recursively
         fib(i - 1) + fib(i - 2)

assert_eq!(fib(7), 13);

The generated code is not completely abstraction free as it uses one dyn trait (without any boxing) to overcome rust's recursive type limitations. In most cases, however, the optimizer should be able to eliminate any dynamic dispatch.

Unfortunately, mutable recursive closures are not supported.



Takes a closure definition where the first parameter will be a Fn to the closure itself. Returns a recursive closure with the same signature, except the first parameter will be eliminated.