ListFilter
Filters a delimted list and returns the values from the callback test This BIF will invoke the callback function for each entry in the list, passing the entry as a string.
If the callback returns true, the entry will be included in the new list.
If the callback returns false, the entry will be excluded from the new list.
If the callback requires strict arguments, it will only receive the entry as a string.
If the callback does not require strict arguments, it will receive the entry as a string, the index (0-based), and the original list as a string.
Parallel Execution
If the parallel
argument is set to true, and no max_threads
are sent, the filter will be executed in parallel using a ForkJoinPool with parallel streams.
If max_threads
is specified, it will create a new ForkJoinPool with the specified number of threads to run the filter in parallel, and destroy it after the operation is complete.
Please note that this may not be the most efficient way to filter, as it will create a new ForkJoinPool for each invocation of the BIF. You may want to consider using a shared ForkJoinPool for better performance.
Method Signature
ListFilter(list=[string], filter=[function:Predicate], delimiter=[string], includeEmptyFields=[boolean], multiCharacterDelimiter=[boolean], parallel=[boolean], maxThreads=[integer])
Arguments
list
string
true
string list to filter entries from
filter
function:Predicate
true
function closure filter test. You can alternatively pass a Java Predicate which will only receive the 1st arg.
delimiter
string
false
string the list delimiter
,
includeEmptyFields
boolean
false
boolean whether to include empty fields in the returned result
false
multiCharacterDelimiter
boolean
false
boolean whether the delimiter is multi-character
true
parallel
boolean
false
Whether to run the filter in parallel. Defaults to false. If true, the filter will be run in parallel using a ForkJoinPool.
false
maxThreads
integer
false
The maximum number of threads to use when running the filter in parallel. If not passed it will use the default number of threads for the ForkJoinPool. If parallel is false, this argument is ignored.
Examples
Example using a simple numeric comparison
Take list and use List Filter to return items that are 3 and higher.
numberList = "1,2,3,4,5,6";
threeOrMore = listFilter( numberList, ( Any item ) => {
return item >= 3;
} );
writedump( threeOrMore );
Result: A List with the values '3,4,5,6'
Example using a member function
This is the same example as above, but using a member function on the list instead of a standalone function.
numberList = "1,2,3,4,5,6";
threeOrMore = numberList.listFilter( ( Any item ) => {
return item >= 3;
} );
writedump( threeOrMore );
Result: A List with the values '3,4,5,6'
Additional Examples
mylist = "Save|Water|Plant|green";
filterResult = listFilter( mylist, ( Any element, Any idx ) => {
if( element != "water" ) {
return true;
}
return false;
}, "|" );
writeDump( filterResult );
// Member Function
listVal = "one,two,three,four,five";
res = listVal.listFilter( ( Any elem, Any ind ) => {
if( elem != "three" ) {
return true;
}
return false;
} );
writeDump( res );
Related
Last updated
Was this helpful?