The following is a naive attempt to increase the concurrency of the Filter function:
fun {Filter L F}
case L of
X|Xs then if thread {F X} end
then X|{Filter Xs F}
else {Filter Xs F} end
else nil
end
end
What is an alternative Filter operation with better concurrency (code)? (Hint : You may make make use of message-passing concurrency.)
The current code evaluates an item of the list, then the next one, sequentially until the end of the list. One way to add concurrency to the code is to divide the logic into two parts: Evaluation and appending to the result list.
Evaluation: Loop through the list and evaluate each item in a separate thread, and store the results in a boolean list.
Append: Loop through the boolean list (concurrently while it is being created) and if the values are true, add to the result list. Skip all the false values.
Example solution: