ListMap
This BIF will iterate over each item in the delimited list and invoke the callback function for each item so you can do any operation on the item and return a new value that will be set at the same index in a new list.
The callback function will be passed the item as a string, the current index (0-based), and the original list.
If the callback requires strict arguments, it will only receive the item as a string.
If the callback does not require strict arguments, it will receive the item as a string, the index (0-based), and the original list.
Parallel Execution
If the parallel
argument is set to true, and no max_threads
are sent, the map 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 map in parallel, and destroy it after the operation is complete.
Please note that this may not be the most efficient way to map, 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
ListMap(list=[string], callback=[function:Function], delimiter=[string], includeEmptyFields=[boolean], parallel=[boolean], maxThreads=[integer])
Arguments
list
string
true
The delimited list to perform operations on
callback
function:Function
true
The function to invoke for each item. The function will be passed 3 arguments: the value, the index, the array. You can alternatively pass a Java Function 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
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
Script Syntax
Rainbow = "Whero, Karaka, Kowhai, Kakariki, Kikorangi, Tawatawa, Mawhero";
externalList = "";
reverseRainbow = listMap( rainbow, ( Any v, Any i, Any l ) => {
var newValue = "#i#:#v.reverse()#";
externalList = externalList.listAppend( newValue );
return newValue;
} );
writeDump( [
{
RAINBOW : rainbow
},
{
REVERSERAINBOW : reverseRainbow
},
{
EXTERNALLIST : externalList
}
] );
Additional Examples
fruits = "apple,pear,orange";
writedump( fruits );
fruitsPlural = listMap( fruits, ( Any value, Any index, Any list ) => {
return value & "s";
} );
writedump( fruitsPlural );
Related
Last updated
Was this helpful?