The sequential implementation of the net asset application looked up the latest price for each symbol one at a time. The major delay is the time spent waiting for the responses from the web—the network delay. Let’s refactor the previous code so we can make the requests for the latest prices for all the symbols concurrently. When done, we should see a faster response from our net asset application.
To make this application concurrent, well, it’s effortless. Since the ticker symbols are all in a collection, all we have to do is turn the collection into a parallel collection and we’re done! We need to change only one line. In the previous code, change
val valuesAndWorth = symbolsAndUnits.keys.map ...