Implementing parallel map and fold with threads
In Chapter 3, Exploring Functions, we discussed two higher-order functions: map
, which applies a function to the elements of a range by either transforming the range or producing a new range, and fold
(also referred to as reduce
), which combines the elements of a range into a single value. The various implementations we did were sequential. However, in the context of concurrency, threads, and asynchronous tasks, we can leverage the hardware and run parallel versions of these functions to speed up their execution for large ranges, or when the transformation and aggregation are time-consuming. In this recipe, we will see a possible solution for implementing map
and fold
using threads.
Getting ready
You need to be familiar with the concepts of the map
and fold
functions. It is recommended that you read the Implementing higher-order functions map and fold recipe from Chapter 3, Exploring Functions. In this recipe, we will use the...