Accessing tuple elements in parallel

In this recipe, we will cover how to access elements of a tuple in parallel.

How to do it…

  1. Import the built-in package as follows:
    import Control.Parallel.Strategies
  2. Evaluate the expression in a tuple in parallel. We perform this task twice with different strategies to demonstrate how strategies are easily swapped to change the parallel nature of the code as follows:
    main = do
      let (a, b) = withStrategy (parTuple2 rseq rseq) (task1, task2)
      print $ seq (a+b) "done 1"
      let (a, bs) = withStrategy (parTuple2 rseq rdeepseq) (task1, tasks)
      print $ seq (a + sum bs) "done 2"
  3. Define time-consuming tasks as follows:
    task1 = 8^8^8 :: Integer
    task2 = 8^8^8 :: Integer
    tasks = [10^10..10^10+10000] :: [Integer]
  4. Compile the code with ...

Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.