Functions : ScalarValuedFunction : Composing Functions
 
Share this page                  
Composing Functions
As noted earlier, a number of functions are predefined by DataFlow. These functions are very specific, generally performing one small task; for example, computing the logical AND of two booleans.
Many times, more complex behavior is required than what is available from these simple functions. To produce more complex behavior, functions can be composed—that is, combined together to produce a new function. This is the same as the concept of function composition in mathematics.
Composing new functions is simple—just pass a function as an argument to a function. Most functions are written to accept other functions as arguments to permit this. The only caveat is that the function being passed must evaluate to a valid type for the argument.
The following is an example of composing a function representing a more complex expression: calculating the total, including taxes, for a line item.
Building complex expressions
static import com.pervasive.datarush.functions.Arithmetic.add;
static import com.pervasive.datarush.functions.Arithmetic.mult;

...
// Compute units * unitCost * (1.0 + taxRate)
ScalarValuedFunction lineSubtotal = mult("units", "unitCost");
ScalarValuedFunction lineTotal = mult(subtotal, add("taxRate", 1.0));
...