Skip to main content
main-content
Top

About this book

Beginning Scala, Second Edition takes a down-to-earth approach to teaching Scala that leads you through simple examples that can be combined to build complex, scalable systems and applications.

This book introduces you to the Scala programming language, its object-oriented and functional programming characteristics, and then guides you through Scala constructs and libraries that allow you to assemble small components into high-performance, scalable systems. You will learn why Scala is judiciously used for critical business applications by leading companies such as Twitter, LinkedIn, Foursquare, the Guardian, Morgan Stanley, Credit Suisse, UBS, and HSBC.

Scala is a multi-paradigm programming language that combines both functional and object-oriented features. Moreover, this highly scalable language lends itself well to building cloud-based/deliverable Software as a Service (SaaS) online applications.

Table of Contents

Chapter 1. Getting Started with Scala

Abstract
Scala is a contraction of the words "scalable" and "language." The scalability of a language is different from the scalability of a program or application. The scalability of a program is defined in terms of its performance, while the scalability of a language has a broad ranging definition, in that the high scalability of Scala is a consequence of the fusion of object-oriented and functional programming. Scala’s functional programming constructs let you compose complex functions from simple functions. Scala's object-oriented constructs let you structure highly adaptable objects through innovations in object-oriented constructs of Java. While Java has undoubtedly ushered in a wave of new era in software development, this initial chapter discusses why Scala supplants Java as a worthy successor and gets you started on a wonderful journey of great distances.
Vishal Layka, David Pollak

Chapter 2. Basics of Scala

Abstract
Scala is a multi-paradigm language. This chapter introduces functional programming concepts in tandem with nonfunctional programming concepts. This chapter is aimed at making you comfortable with the building blocks of the Scala language. Before embarking on an in-depth journey of Scala, this chapter introduces the basic concepts of Scala that should give you enough understanding to enable you to write useful programs. To that end, we had to leave out some details. We do cover the detailed explanation in the subsequent chapters.
Vishal Layka, David Pollak

Chapter 3. Object Orientation in Scala

Abstract
The essence of OOP is modelling abstractions for handling complexities in software development, by means of classes and objects. Finding right abstractions, however, remains an arduous quest. An object in software is an abstraction of a real-world object, comprising essential properties and behaviors that set it apart from other objects. OOP is successful because object-oriented languages implement a number of principles such as encapsulation, inheritance, and polymorphism that make the software design and construction process much simpler and elegant when compared to other antiquated approaches. To illustrate these principles we concoct an abstraction with the aid of a classic example of shapes:
Vishal Layka, David Pollak

Chapter 4. Functional Programming in Scala

Abstract
In the non-fiction work Old Times on the Mississippi, Mark Twain wrote: "When I was a boy of 14, my father was so ignorant I could hardly stand to have the old man around. But when I was 21, I was astonished at how much the old man had learned in seven years". Functional programming is the old man that comes to the rescue when writing robust concurrent software. Functional programming treats computation as the evaluation of mathematical functions and avoids state and mutable data. It is a declarative programming paradigm, in which programming is done with expressions. The imperative style of programming emphasizes sequence of operations characterized by iteration with loops, mutating data in place, and methods with side effects where the order of side effects is critical toward the right effect. The basic constructs in an imperative language, such as Java, are imperative statements that change the state of a program, as illustrated here:
Vishal Layka, David Pollak

Chapter 5. Pattern Matching

Abstract
So far, we’ve explored some of the basic functional cornerstones of Scala: immutable data types and the passing of functions as parameters. The third cornerstone of functional programming is pattern matching. Pattern matching provides a powerful tool for declaring business logic in a concise and maintainable way. Scala blends traditional functional programming pattern matching with object-oriented concepts to provide a very powerful mechanism for writing programs. In this chapter, we’re going to explore the basics of pattern matching. Then we’re going to see how Scala’s case classes bridge between object-oriented data encapsulation and function decomposition. Next, we’ll see how Scala’s pattern-matching constructs become functions that can be passed around and composed. Let's look at the simple example first.
Vishal Layka, David Pollak

Chapter 6. Scala Collections

Abstract
The collections framework in Scala is a high-performance and type-parametrized framework with support for mutable and immutable type hierarchies. These distinct and independent mutable and immutable type hierarchies enable switching between mutable and immutable implementations much simpler. Scala’s object-oriented collections also support functional higher-order operations such as map, filter, and reduce that let you use expression-oriented programming in collections.  You can access and use the entire Java collections library from your Scala code because Scala is a JVM language, but this is not recommended because higher-order operations are not available with Java Collections library. Scala has a rich collection library. This chapter gives you a tour of the most commonly used collection types and operations, showing just the types you will use most frequently. The goal of this chapter is to guide you through the myriad of options to find the solutions you need.
Vishal Layka, David Pollak

Chapter 7. Traits

Abstract
In this chapter you will learn how to construct reusable parts of a program and deal with the tribulations of multiple inheritance, sidestepping the disadvantages of single inheritance by means of the mixing of compositions made possible by traits.
Vishal Layka, David Pollak

Chapter 8. Scala Type System

Abstract
Types in a programming language are checked at compile time and can be inferred by a compiler. Scala has a strong and statically typed language with a unified Type system. The two fundamental design considerations of a programming language are static versus dynamic typing and strong versus weak typing.
Vishal Layka, David Pollak

Chapter 9. Scala and Java Interoperability

Abstract
One of the main design goals of Scala is running on a JVM and providing interoperability with Java. The need for Scala and Java interoperability arises when you want to use existing Java libraries or framework. Scala code is often used in tandem with large Java programs and frameworks. Even though integration with Java is easy for the most part, we encourage you to use pure Scala as much as possible. When you are working with the Java library or framework, first try to find something equivalent in Scala, and use Java if there’s no equivalent Scala library available.
Vishal Layka, David Pollak

Chapter 10. DSL and Parser Combinator

Abstract
The dichotomy of generic and specific manifests itself in the programming sphere. Domain-specific languages (DSLs) are one of the forms of the manifestations of this dichotomy. Domain-specific languages are just what they are called: domain specific. All programming languages are domain-specific languages when they come into existence, but that changes as they evolve. Domain-specific languages are created to solve problems in a certain area (or more precisely, in a certain domain) but as they gradually evolve to solve problems in several domains, the line that distinguishes them as specific blurs. Thus, such a language transgresses from the specific to the generic. A DSL is a special purpose language, the other extreme of which is a general purpose language, such as Scala and Java. Unfortunately, these general purpose languages have drawbacks, for example, if you want to execute a task on a database, then it is necessary to write a computer program for executing this task using a general purpose language. However, a DSL could be used to perform a number of such tasks on a database. And this is why some experts regard SQL, the structured query language, as a DSL.
Vishal Layka, David Pollak

Chapter 11. Simple Build Tool - SBT

Abstract
The software development typically includes activities such as compiling source code into binary code, executing tests, packaging binary code into archives, and deploying archives to production systems. A build is a term that refers to compiled code packaged and deployed on production. A build is a technical term for a deliverable. Deliverable is the set of executable code expected by the stakeholders. The process through which a build has to go to become the deliverable is called a build process. Thus a build process, in general, is comprised of compilation, testing, packaging, and deployment.
Vishal Layka, David Pollak

Chapter 12. Scala for Web Application

Abstract
The web continues to evolve in a sort of benign continuum and has become the central town of all applications with its architecture transcending to real-time, due to the emerging new requirements such as asynchrony, reactivity and responsivity, collaborating with both structured and unstructured datastore and so on. The aforementioned factors have made web development a non-trivial undertaking, which means several web frameworks have emerged to address these issues. In this chapter, we begin our journey of the Scala web landscape through one of the highly popular web frameworks Play 2. We elected Play framework, because it enables web development for the modern era using both Scala and Java. We introduce both Play 2 for Scala and Play 2 for Java, because we believe that most Scala developers are experienced in Java through industrial projects or academia. The remaining Scala developers are the ones who transitioned to Scala from Java and so, we believe, having familiarity with Java will make the transition to Play framework much easier and it allows Play framework to position itself in an already established segment of Java web.
Vishal Layka, David Pollak

Chapter 13. Scala Best Practices

Abstract
Thanks for hanging in and reading all this way. We’ve covered a lot of ground so far. We’ve discussed the Scala language and developed a collection of idioms for building applications using Scala. We’ve explored how Scala can be used by different team members in different ways. We’ve seen how Scala allows you to compose fine-grained pieces of code together into complex systems that work well together.
Vishal Layka, David Pollak
Additional information