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

2550

Comments

  1. .race runs the doublings in parallel but, unlike .hyper, does not promise to return them in order.

  2. That is fine here because summing is order-independent: the total is 2550 — twice the sum of 1 to 50. When you only combine the results (sum, count), .race is the natural choice and can carry a little less overhead than .hyper.

Course navigation

A racing sum   |   Hyper with a filter