Acknowledgments

Many people have contributed to what’s good in this book. The problems and errors that remain are the authors’ alone. Thanks to everyone at the Pragmatic Bookshelf. Thanks to Dave Thomas and Andy…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Parallel Collections

Pragmatic Scala — by Venkat Subramaniam (102 / 140)

👈 The Ultimately Lazy Streams | TOC | Wrapping Up 👉

If laziness is a road to efficiency, parallelism may be considered a flight to that destination. If two or more tasks can be executed in any sequence without any impact on the correctness of the result, then those tasks may very well be run in parallel. Scala provides a few different ways to achieve that. The easiest of those is parallel processing of elements in a collection.

We work with collections of data all the time. We may need to check the price of several products, update inventories based on the orders fulfilled, or tally up payments for recent transactions. When we have a collection of data to work with, we often use internal iterators like map, filter, and foldLeft (we used a few of these in Chapter 8, Collections) to perform the necessary operations and produce the desired results.

If the number of objects or items is large and/or the time to process each one of them is long, the overall response time to produce the result may become prohibitively high. Parallelizing these tasks to run on multiple threads and making use of multiple cores can tremendously improve the speed. But using low-level threading constructs and locks, unfortunately, can increase accidental complexity and the resulting concurrency-related errors, sucking the life out of programmers. Thankfully, you don’t have to endure that in Scala. It’s easy to parallelize operations on a collection of data.

Next we’re going to implement an example to work with a collection of data sequentially, and then we’ll parallelize that to improve speed.

Let’s take an example and implement it first sequentially and then refactor that to make it faster. We’ll use an example to collect and display weather data — globetrotters keenly keep an eye on the weather in the cities they’re heading to. Let’s create a little program that will report the temperature and weather conditions in select cities.

We’ll start with a list of city names, fetch the current weather condition, and report the details in sorted order by city. A request to the OpenWeatherMap’s web service[5] will get us the data in different formats. We’ll use the XML format since it’s easy to parse that in Scala. We’ll also report the time it takes to create…

Add a comment

Related posts:

Aiming for the Right Target

Most software is designed for the development lab or the testers in the QA department. It is designed and built to pass tests such as, “The customer’s first and last names are required, but the…

Minimal Configuration

To follow the examples in this book, you’ll need to make sure that ‘nocompatible’ is set and that filetype detection is enabled. Prior to version 8 of Vim, you had to specify these settings in your…

How high will the price of Bitcoin go in 10 days?

Two present factors support the widespread idea that Bitcoin’s price will increase beyond $50,000 in the next 10 days. Bitcoin’s value is expected to rise to $ 50,000 in the next 10 days. According…