Higher order functions
$map
Signature: $map(array, function)
Parameters:
array
- An array to map from.function
- The function that is supplied as the second parameter must have the following signature:
function(value [, index [, array]])
Each value in the input array is passed in as the first parameter in the supplied function. The index (position) of that value in the input array is passed in as the second parameter, if specified. The whole input array is passed in as the third parameter, if specified.
Returns an array containing the results of applying the function
parameter to each value in the array
parameter.
The function that is supplied as the second parameter must have the following signature:
function(value [, index [, array]])
Each value in the input array is passed in as the first parameter in the supplied function. The index (position) of that value in the input array is passed in as the second parameter, if specified. The whole input array is passed in as the third parameter, if specified.
Examples
Expression | Result |
---|---|
$map([1..5], $string) | ["1", "2", "3", "4", "5"] |
With user-defined (lambda) function:
evaluates to:
$filter
Signature: $filter(array, function)
Parameters:
array
- An array to filter.function
- The function that is supplied as the second parameter must have the following signature:
function(value [, index [, array]])
Each value in the input array is passed in as the first parameter in the supplied function. The index (position) of that value in the input array is passed in as the second parameter, if specified. The whole input array is passed in as the third parameter, if specified.
Returns an array containing only the values in the array
parameter that satisfy the function
predicate (i.e. function
returns Boolean true
when passed the value).
The function that is supplied as the second parameter must have the following signature:
function(value [, index [, array]])
Each value in the input array is passed in as the first parameter in the supplied function. The index (position) of that value in the input array is passed in as the second parameter, if specified. The whole input array is passed in as the third parameter, if specified.
Example The following expression returns all the products whose price is higher than average:
$single
Signature: $single(array, function)
Parameters:
array
- An array to search in.function
- The function that is supplied as the second parameter must have the following signature:
function(value [, index [, array]])
Each value in the input array is passed in as the first parameter in the supplied function. The index (position) of that value in the input array is passed in as the second parameter, if specified. The whole input array is passed in as the third parameter, if specified.
Returns the one and only one value in the array
parameter that satisfy the function
predicate (i.e. function
returns Boolean true
when passed the value). Throws an exception if the number of matching values is not exactly one.
The function that is supplied as the second parameter must have the following signature:
function(value [, index [, array]])
Each value in the input array is passed in as the first parameter in the supplied function. The index (position) of that value in the input array is passed in as the second parameter, if specified. The whole input array is passed in as the third parameter, if specified.
Example The following expression the product in the order whose SKU is "0406654608"
:
$reduce
Signature: $reduce(array, function [, init])
Parameters:
array
- An array to reduce.function
- Thefunction
must accept at least two arguments, and behaves like an infix operator between each value within thearray
. The signature of this supplied function must be of the form:
function(accumulator, value[, index[, array]])
Returns an aggregated value derived from applying the function
parameter successively to each value in array
in combination with the result of the previous application of the function.
The function
must accept at least two arguments, and behaves like an infix operator between each value within the array
. The signature of this supplied function must be of the form:
myfunc($accumulator, $value[, $index[, $array]])
Example
This multiplies all the values together in the array [1..5]
to return 120
.
If the optional init
parameter is supplied, then that value is used as the initial value in the aggregation (fold) process. If not supplied, the initial value is the first value in the array
parameter.
$sift
Signature: $sift(object, function)
Parameters:
object
- An object to process.function
- Thefunction
that is supplied as the second parameter must have the following signature:
function(value [, key [, object]])
Each value in the input object is passed in as the first parameter in the supplied function. The key (property name) of that value in the input object is passed in as the second parameter, if specified. The whole input object is passed in as the third parameter, if specified.
Returns an object that contains only the key/value pairs from the object
parameter that satisfy the predicate function
passed in as the second parameter.
If object
is not specified, then the context value is used as the value of object
. It is an error if object
is not an object.
The function that is supplied as the second parameter must have the following signature:
function(value [, key [, object]])
Each value in the input object is passed in as the first parameter in the supplied function. The key (property name) of that value in the input object is passed in as the second parameter, if specified. The whole input object is passed in as the third parameter, if specified.
Example
This sifts each of the Product objects such that they only contain the fields whose keys start with the string “Product” (using a regex). This example returns:
Was this page helpful?