compose
First introduced in version: 3.00.3
Syntax
compose(first, second)
Details
The compose function creates a composite function from two
functions, equivalent to second(first()). The logic works as
follows:
-
Call the first function with all the provided arguments.
-
Take the result from first and pass it as the only argument to the second function.
-
Return the result of
second(first(...)).
For example, for the composite function f = compose(add, sin),
f(x, y) will return the same result as sin(add(x,
y)).
Use cases: The compose function is used to combine multiple
functions into a new function that executes them in sequence. It is suitable for
data processing pipelines, function reuse, logic encapsulation, and functional
programming scenarios, improving code readability and maintainability while
simplifying complex logic.
Parameters
first indicates the first function to be called, which accepts one or more arguments.
second indicates the second function to be called, which accepts only one argument (the return value of first).
Returns
A new function (of type FUNCTIONDEF) is returned, with the same parameters as the first function.
Examples
g = def(x, y, z) { return x * y + z }
f = def(x) { return abs(x * 3) }
compo_func = compose(g, f) // Equivalent to f(g(*))
compo_func(-1, 5, 3) // Output: 6
