ArraySome
Used to iterate over an array and test whether ANY items meet the test callback.
The function will be passed 3 arguments: the value, the index, and the array. You can alternatively pass a Java Predicate which will only receive the 1st arg. The function should return true if the item meets the test, and false otherwise.
Note: This operation is a short-circuit operation, meaning it will stop iterating as soon as it finds the first item that meets the test condition.
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 iterate, 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
ArraySome(array=[array], callback=[function:Predicate], parallel=[boolean], maxThreads=[integer])
Arguments
array
array
true
The array to reduce
callback
function:Predicate
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 Predicate which will only receive the 1st arg.
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
Simple Example
// Create an array
arrayList = [
"apple",
"pineapple",
"mango",
"apricot"
];
// Match some
result = arraySome( arrayList, ( Any fruit ) => {
return fruit.startsWith( "a" );
} );
// Print result
writeOutput( (result ? "Some" : "No") & " matches were found!" );
Result: Some matches were found!
Member Function Example
// Create an array
arrayList = [
"apple",
"pineapple",
"mango",
"apricot"
];
// Match some
result = arrayList.some( ( Any fruit ) => {
return fruit.endsWith( "a" );
} );
// Print result
writeOutput( (result ? "Some" : "No") & " matches were found!" );
Result: No matches were found!
Additional Examples
newArray = [
"a",
"b",
"c",
"b",
"d",
"b",
"e",
"f"
];
dump( newArray );
hasSome1 = arraySome( newArray, ( Any item, Any idx, Any arr ) => {
return item == "b";
} );
dump( hasSome1 );
// member function
hasSome2 = newArray.some( ( Any item, Any idx, Any arr ) => {
return item == "k";
} );
dump( hasSome2 );
Related
Last updated
Was this helpful?