Book Image

Professional Scala

By : Mads Hartmann, Ruslan Shevchenko
Book Image

Professional Scala

By: Mads Hartmann, Ruslan Shevchenko

Overview of this book

This book teaches you how to build and contribute to Scala programs, recognizing common patterns and techniques used with the language. You’ll learn how to write concise, functional code with Scala. After an introduction to core concepts, syntax, and writing example applications with scalac, you’ll learn about the Scala Collections API and how the language handles type safety via static types out-of-the-box. You’ll then learn about advanced functional programming patterns, and how you can write your own Domain Specific Languages (DSLs). By the end of the book, you’ll be equipped with the skills you need to successfully build smart, efficient applications in Scala that can be compiled to the JVM.
Table of Contents (12 chapters)


Scala is a type-safe JVM language that incorporates both object-oriented and functional programming into an extremely concise, logical, and extraordinarily powerful language. Some may be surprised to know that Scala is not quite as new as they may have thought, having first been introduced in 2003. However, it is in the past few years in particular that Scala has begun to develop a significant following.

This book enables you to build and contribute to Scala programs, recognizing common patterns and techniques used with the language.

This is a practical book which provides you with a lot of hands-on experience with Scala.

Who This Book Is For

This book is for developers who are interested in learning about the advanced features of the Scala language. Basic knowledge of the Scala programming language is required to follow the instructions in this book.

What This Book Covers

Chapter 1, Setting up the Development Environment, shows you how to set up your development environment. You'll learn the basics of Scala, such as what the simple Scala program looks like and what a typical developer flow is. It'll also cover some aspects of testing your Scala program by using unit testing.

Chapter 2, Basic Language Features, covers classes and objects, traits, pattern matching, case class, and so on. You'll also implement your chatbot application by applying the object oriented concepts.

Chapter 3, Functions, covers functional programming with Scala and how object- oriented and functional approaches complete each other. You'll identify generic classes and also identify how to create user-defined pattern matching and why is it useful.

Chapter 4, Scala Collections, teaches you how to work with lists. Then, it covers some more relevant data structures. Finally, you'll look at how collections relate to monads and how you can use that knowledge to make some powerful abstractions in your code.

Chapter 5, Scala Type System, covers the type system and polymorphism. It'll also enable you to identify the different types of variance, which provides a way to constrain parameterized types. Then, you'll cover some advanced types such as abstract type members and option.

Chapter 6, Implicits, covers implicit parameters and implicit conversions. You'll be learning about how they work, how to use them, and what kind of benefits and perils they provide.

Chapter 7, Functional Idioms, covers the core concepts of functional programming like Pure functions, immutability, and higher-order functions. It'll also cover two popular functional programming libraries called Cats and Doobie, and use them to write some interesting programs.

Chapter 8, Domain Specifc Languages, covers how Scala makes it possible to write powerful DSLs by providing a few interesting language features. Then, it'll cover a DSL that you'll very likely be using if you're going to work with Scala professionally.

Finally, you'll implement your own DSL.

What You Need for This Book

The minimum hardware requirements are as follows:

  • Intel Core i3 processor

  • 2 GB RAM

  • An Internet connection

Please ensure you have the following software installed on your machine:

  • Microsoft Windows 10/8/7 (64 bit)

  • JDK 8

  • IntelliJ + Scala plugin


  • macOS 10.5 or higher (64-bit)

  • JDK 8

  • IntelliJ + Scala plugin


  • Linux 64-bit

  • KDE, GNOME, or Unity DE desktop

  • JDK 8

  • IntelliJ + Scala plugin


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows:

Here, we define an immutable value with the name name, which keeps the user's  stdin.

The main method is an essential part of any Scala program.

A block of code is set as follows:

package com.packt.courseware
object Chatbot1
   def main(args: Array[String]):Unit =  {
     // do something

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: " Select Run sbt-test."

Important new programming terms are shown in bold. Conceptual terms are shown in italics.


Important additional details about a topic appear like this, as in a sidebar.


Important notes, tips, and tricks appear like this.

Installation and Setup

Before we start this book, we'll install IntelliJ IDE.

Installing IntelliJ IDE

  1. Visit in your browser.

  2. Click on the DOWNLOAD button on the web page.

  3. Choose your appropriate OS and, under Community, click on the Download option.

  4. Follow the steps in the installer and that's it! Your IntelliJ IDE is ready.

Installing Scala plugin (make heading)

  1. Open IntelliJ IDEA.

  2. Go to File Menu.

  3. In the file menu, select Plugins.

  4. Click on Browse repositories button and enter Scala.

  5. Select Scala plugin to install it.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.