Lambdas & First Class Procedures
Claro opens you up to taking full advantage of functional programming techniques by allowing you to assign Procedures to variables and to pass them around as data, allowing you to hand them off to be called later.
Defining Lambdas
Lambdas expressions look something like the examples below.
Fig 1:
var f: function<int -> int> = x -> x + 1;
var c: consumer<int> = x -> { print(x); };
var p: provider<int> = () -> 10;
Note: lambdas require explicit type annotations , but Claro does support an alternative syntax sugar to bake the type annotation directly into the lambda expression:
Fig 2:
var add = (lhs: int, rhs: int) -> int { return lhs + rhs; };
First Class Procedure References
You may also reference named procedures as first-class data just like lambdas:
Fig 3:
function add(x: int, y: int) -> int {
return x + y;
}
var applyBiConsumer =
lambda (x: int, y: int, mapFn: function<|int, int| -> int>) -> {
print(mapFn(x, y));
};
# Pass a reference to the `add()` function as a first class arg.
applyBiConsumer(10, 5, add); #15.
Output:
15