This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/rhai_engine/rhaibook/language/fn-curry.md
2025-04-03 09:18:05 +02:00

1.0 KiB

Function Pointer Currying

{{#include ../links.md}}

It is possible to curry a [function pointer] by providing partial (or all) arguments.

Currying is done via the curry keyword and produces a new [function pointer] which carries the curried arguments.

When the curried [function pointer] is called, the curried arguments are inserted starting from the left.

The actual call arguments should be reduced by the number of curried arguments.

fn mul(x, y) {                  // function with two parameters
    x * y
}

let func = mul;                 // <- de-sugars to 'Fn("mul")'

func.call(21, 2) == 42;         // two arguments are required for 'mul'

let curried = func.curry(21);   // currying produces a new function pointer which
                                // carries 21 as the first argument

let curried = curry(func, 21);  // function-call style also works

curried.call(2) == 42;          // <- de-sugars to 'func.call(21, 2)'
                                //    only one argument is now required