Book Image

Learning Scala Programming

By : Vikash Sharma
Book Image

Learning Scala Programming

By: Vikash Sharma

Overview of this book

Scala is a general-purpose programming language that supports both functional and object-oriented programming paradigms. Due to its concise design and versatility, Scala's applications have been extended to a wide variety of fields such as data science and cluster computing. You will learn to write highly scalable, concurrent, and testable programs to meet everyday software requirements. We will begin by understanding the language basics, syntax, core data types, literals, variables, and more. From here you will be introduced to data structures with Scala and you will learn to work with higher-order functions. Scala's powerful collections framework will help you get the best out of immutable data structures and utilize them effectively. You will then be introduced to concepts such as pattern matching, case classes, and functional programming features. From here, you will learn to work with Scala's object-oriented features. Going forward, you will learn about asynchronous and reactive programming with Scala, where you will be introduced to the Akka framework. Finally, you will learn the interoperability of Scala and Java. After reading this book, you'll be well versed with this language and its features, and you will be able to write scalable, concurrent, and reactive programs in Scala.
Table of Contents (21 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Parallel collections


Well, before discussing parallel collections in Scala, it's important to have some insight about what parallel computation is. How's it different from concurrent and asynchronous?

Well, we have spent some time understanding that asynchronous computation is non-blocking, hence we know that async computation happens from outside of the main program flow and gives you the value once the computation gets completed. To understand the difference between concurrent and parallel computation, let's look at the following example:

In the example, we are given a collection of numbers where we want to apply a function to each element of the collection to get a new collection. One method is to take a value out from the starting collection, add one to it, and put that value in a new collection, until the first collection is empty. Now, this process can be made faster by introducing two threads to perform the task of adding one to an element of the collection; let's put it another way...