<< hypermatrices types library >>

Scilab Help >> Scilab > types > Lambda functions

Lambda functions

Scilab procedures and Scilab objects

Description

Lambdas or anonymous functions are a type of Scilab functions.

Lambda definition

Mainly used as "one liner" function to avoid to declare a function. Or when it is used once. Lambdas are the ability to catch variables states at declaration unlike classic functions. Lambdas can be assign to a variable at the same time of declaration, so they can be passed directly as function parameter.

Calling function

Miscellaneous

Lambdas are Scilab objects (with type numbers 13). And they can be manipulated (built, saved, loaded, passed as arguments,...) as other variable types.

Examples

//simple use
pyth = #(x, y) -> (sqrt(x ^ 2 + y ^ 2));

pyth(3, 4)
pyth(12, 16)

//catching of variable
y = 4;
pyth = #(x) -> (sqrt(x ^ 2 + y ^ 2));
clear y;

pyth(3)

//used as function parameter

t = 0:0.1:%pi;
y = ode(0, 0, t, #(t, y) -> (
    y ^ 2 - y * sin(t) + cos(t)
));

plot(t, y);

//lambda factory

function f=comp(threshold)
    f = #(x) -> (x < threshold);
end

//return a lambda configured by the input variable
comp_10 = comp(10);
[comp_10(3), comp_10(15), comp_10(22)]
comp_10([3 15 22])

//return another lambda configured by the input variable
comp_20 = comp(20);
[comp_20(3), comp_20(15), comp_20(22)]
comp_20([3 15 22])

See also

History

VersionDescription
2025.0.0 lambda introduction.

Report an issue
<< hypermatrices types library >>