concept parallel in category functional programming

appears as: parallel
Functional Programming in C#: How to write better C# code

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.

Here you define nums to be a list of all integers between 10,000 and -10,000; their sum should obviously be 0. You then create two tasks: task1 computes and prints out the sum; task2 first sorts the list and then computes and prints the sum. Each of these tasks will correctly compute the sum if run independently. When you run both tasks in parallel, however, task1 comes up with an incorrect and unpredictable result.

Listing 2.5. A pure implementation that executes in parallel

We’ll then address some frequently recurring concerns when working with Tasks: combining several asynchronous operations, handling failure, performing multiple retries, and running tasks in parallel.

13.1.7. Running asynchronous operations in parallel

Because Task is used to represent operations that take time, it’s only natural that you may want to execute them in parallel when possible. Imagine you want to check prices offered by different airlines. Assume you have several classes encapsulating access to the airlines’ APIs, each implementing the Airline interface:

Listing 13.7. Performing two Tasks in parallel with Apply
Task<Flight> BestFareA(string from, string to, DateTime on) => Async(PickCheaper) .Apply(ryanair.BestFare(from, to, on)) .Apply(easyjet.BestFare(from, to, on)); static Func<Flight, Flight, Flight> PickCheaper = (l, r) => l.Price < r.Price ? l : r;

In this version, the two calls to BestFare are kicked off independently, so they run in parallel, and the total time needed for BestFareA to complete is determined by the time required for the longer of the API calls to complete—not their sum.

Functional Programming in Java: How functional techniques improve your Java programs

This is an excerpt from Manning's book Functional Programming in Java: How functional techniques improve your Java programs.

Sometimes you need to split a list into two parts at a specific position. Although the singly linked list is far from ideal for this kind of operation, it’s relatively simple to implement. Splitting a list has several useful applications, among which is processing its parts in parallel using several threads.

8.4.3. Processing sublists in parallel

To process the sublists in parallel, you’ll need a special version of the method to execute, which will take as an additional parameter an ExecutorService configured with the number of threads you want to use in parallel.

Figure 14.1. The Main actor produces the main task and sends it to the Manager actor, which splits it into subtasks that are processed in parallel by several Worker actors. Sub results are sent back to the Manager, which passes them to the Receiver. After collating the sub results, the Receiver sends the final result to the Main actor.