Partial Application

Partial application fixes part or all of the parameters of a function to generate a new function with fewer parameters.

Syntax

<functionName>{parameters}

Details

To fix the first few parameters, we do not need to indicate the remaining parameters; if there are any free parameters before a fixed parameter, the positions of these free parameters must be indicated. Please refer to the examples below.

Partial application can be used with template functions that have certain requirements about function inputs.

Examples

$ a=100
$ g=add{a*a};
$ g(8);
10008

$ add{a*a}(88);
10088

$ def f(a,b):a*exp(b)
$ g=f{10};  // g(b)==f(10,b)

$ g(0);
10

$ g(1);
27.182818

$ k=f{,1};  // k(a)==f(a,1)
$ k(10);
27.182818

If there is a free optional parameter before a fixed parameter, and its position is not indicated, it must be specified when the partially applied function is called.

//Calculate the singular value decomposition of matrix m.
m=matrix([[2,1,0],[1,3,1],[0,1,4],[1,2,3]]);
//The syntax of function svd is svd(obj, [fullMatrices=true], [computeUV=true]). Since f1 does not have the optional *fullMatrices* parameter fixed, you must specify *fullMatrices* when executing f1, otherwise an error will be reported.
f1 = svd{m, computeUV=true}
f1(false)   //Successfully executed.
f1()  //An error is reported: The function [svd] expects 1 argument(s), but the actual number of arguments is: 0