filter<functionName>(__iterable);

function functionName(__value) {}

Description

Filters an iterable according to a function. Returns an iterable containing all elements for which the function returned true.

The function argument should be a function accepting a single argument.

When the argument to filter is an OBJECT, the filtering function will be passed an object with a single key-value pair. 

Example

The first example shows how the filter function can be applied and the result can be used as an iterator. The second example shows how the results from the filter can be printed, by converting the iterator result into a list, using collect.

 

use System;

var evenNumbers = filter<isEven>([1,2,3,4,5,6,7,8,9,10]);
printValues(evenNumbers);

// Checks if a number is even
function isEven(item) {
    return item%2 == 0;
}

// Print values from iterator
function printValues(vals) {
    foreach(val in vals) {
        System.print(val :: " is an even value");
    }
}

 

use System;

var evenValuesIterator  = filter<isEven>([1,2,3,4,5]);
var evenValuesList      = collect(evenValuesIterator);
System.print(evenValuesList);                           // Prints [2,4]

// Checks if a number is even
function isEven(item) {
    return item%2 == 0;
}