#recursion #closures #macro #y-combinator #fixpoint

fix_fn

Macro to create recursive closures (similar to the Y combinator)

4 releases (stable)

1.0.2 May 31, 2020
0.1.0 May 31, 2020

#2125 in Rust patterns

Download history 114/week @ 2024-12-10 12/week @ 2024-12-17 90/week @ 2024-12-31 28/week @ 2025-01-07 37/week @ 2025-01-14 85/week @ 2025-01-21 12/week @ 2025-01-28 35/week @ 2025-02-04 96/week @ 2025-02-11 238/week @ 2025-02-18 129/week @ 2025-02-25 141/week @ 2025-03-04 53/week @ 2025-03-11 14/week @ 2025-03-18 16/week @ 2025-03-25

235 downloads per month
Used in forward_goto

MIT license

8KB
97 lines

Build Creates.io Docs

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 {
           i
    } else {
        // fib will call the closure recursively
        fib(i - 1) + fib(i - 2)
    }
});

assert_eq!(fib(7), 13);

No runtime deps