Course of Raku / Functional, concurrent, reactive, and web programming / Concurrent programming / Hyper and race 🆕 / Exercises / A racing sum
Solution: A racing sum
Here is a possible solution to the task.
Code
say (1..50).race.map(* * 2).sum;🦋 You can find the source code in the file parallel-sum.raku.
Output
2550Comments
.raceruns the doublings in parallel but, unlike.hyper, does not promise to return them in order.That is fine here because summing is order-independent: the total is
2550— twice the sum of1to50. When you only combine the results (sum, count),.raceis the natural choice and can carry a little less overhead than.hyper.