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

Preface

Scala, despite being a general purpose JVM-based language like Java, provides an edge with functional programming primitives. Scala has a rich type system, which makes it more expressive. Using type systems, developers can write applications that are less error prone at runtime. Once you have spent enough time learning and practicing concepts, working with Scala is fun. Rich constructs and abstractions have made it powerful, and even framework and library developers enjoy working with Scala.

Keeping these points in mind, this book provides you with all the essentials you need to write programs using Scala. It has all the basic building blocks someone new to Scala might want to know about it, along with the reasons for using it. One of the major goals of this book is to enable you to choose a particular construct above another.

This book will be a companion on your journey towards learning and writing good software using Scala. We believe it'll be a fun journey for you all.

Who this book is for

This book is for programmers who want to get to grips with Scala to write concurrent, scalable, and reactive programs. No prior experience with any programming language is required to learn the concepts explained in this book. However, knowledge of any programming language will help the reader to understand concepts more quickly.

What this book covers

Chapter 1, Getting Started with Scala Programming, introduces you to programming paradigms and explains why choosing Scala gives you an edge over other languages. This chapter ends with demonstrating our first Scala program.

Chapter 2, Building Blocks of Scala, explains the basic constructs required to write a simple program. We'll discuss data types, variables, and literals, with a little bit on type inference.

Chapter 3, Shaping our Scala Program, takes you through looping constructs, control structures, and, most importantly, pattern matching.

Chapter 4, Give Meaning to Programs with Functions, discusses the important functions, function literals, and function call mechanisms. We'll end the chapter with a discussion on partial functions.

Chapter 5, Getting Familiar with Scala Collections, introduces you to Scala's rich collections. We'll discuss mutable and immutable collections, and talk about choosing the most suitable collection.

Chapter 6, Object-Oriented Scala Basics, introduces you to object-oriented Scala. We'll talk about objects, classes, and case classes.

Chapter 7, Next Steps in Object-Oriented Scala, talks about default and parameterized constructors. We will also be introduced to class fields, inheritance, and traits.

Chapter 8, More on Functions, differentiates between functions and methods. We'll talk about partially applied functions, closures, and higher-order functions.

Chapter 9, Using Powerful Functional Constructs, covers some of the important topics in Scala, such as tail-call optimization and currying. Then, we'll discuss combinators and end the chapter with an introduction to parameterized types.

Chapter 10, Advanced Functional Programming, takes you into depth of variance and types. We'll also discuss abstract versus parameterized types.

Chapter 11, Working with Implicits and Exceptions, discusses these two important topics, though they are not related, but are essential to the language. An introduction to implicits and understanding how they work will be the key points of this chapter. We'll also discuss the way we handle exceptions in Scala.

Chapter 12, Introduction to Akka, discusses one of the most talked about toolkits the Scala ecosystem provides. Akka is based on the Actor Model, and that's the main attraction of this chapter.

Chapter 13, Concurrent programming in Scala, takes you deeper into concurrent programming. We'll also cover asynchronous programming using Futures and Promises.

Chapter 14, Programming with Reactive Extensions, talks about reactive programming and the API available in Scala for reactive programming. We'll discuss RxScala, a reactive extension, with a few examples.

Chapter 15, Testing in Scala, covers testing. Our approach will be test-driven. We'll use ScalaTest and try out the library to write test cases for our Scala programs.

To get the most out of this book

This book is an introduction to the Scala language, where you start from learning the basic building blocks of the language and end up learning about frameworks written in Scala. Regardless of whether Scala is your first or second programming language, you're going to enjoy the book. No prior programming knowledge is required. This book has been written with the intention that its chapters are read sequentially, but if you're aware of some of the concepts and want to go skip a particular topic, feel free to do so.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Learning-Scala-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "In Scala, Unit is the same as the void, it does not represent any type."

A block of code is set as follows:

object First {
  def main(args: Array[String]): Unit = {
  val double: (Int => Int) = _ * 2
    (1 to 10) foreach double .andThen(println)
  }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

package lsp

object FirstApp extends App {
  val double: (Int => Int) = _ * 2
  (1 to 10) foreach double .andThen(print)
}

Any command-line input or output is written as follows:

sbt console

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Click on the Create New Project function."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.