Hands-on Scala Programming

Hands-on Scala teaches you how to use the Scala programming language in a practical, project-based fashion. This book is designed to quickly teach an existing programmer everything they need to build all sorts of production applications, taking you from "hello world" to building interactive websites, massively-parallel web crawlers, and distributed file synchronizers in Scala.

Hands-on Scala is coming soon, Summer 2020!

Subscribe for updates!Browse free chapters
G cluster_0 Main Process cluster_1 Sub Process os.watch os.watch SyncActor SyncActor os.watch->SyncActor 1. changed paths Agent Agent SyncActor->Agent 2. StatPath SyncActor->Agent 5. WriteOver HashActor HashActor Agent->HashActor 3. StatInfo HashActor->SyncActor 4. StatAndHashInfo                              

Focused on Real-world Projects

Hands-on Scala is designed for professional developers who need to get up to speed using Scala in production. This book dives straight into use cases: you will write interactive websites, network file synchronizers, parallel web crawlers, data migration tools, and much more. Every chapter not only teaches language concepts, but also walks you through a use case that the author has worked on professionally and will be a valuable addition to your developer toolbox.


Beyond the Scala Language

Knowing the language alone isn't enough to go to production: Hands-on Scala introduces the reader to the ecosystem of editors, build tools, web frameworks, database libraries, everything necessary to do real work using Scala. You will finish this book having all the necessary building blocks to be productive using Scala in production.


Ruthlessly Curated

Hands-on Scala has been rigorously reviewed by top Scala experts and beta-tested on a wide variety of individuals, to ensure an optimal path for anyone learning the Scala language regardless of their programming background. Every chapter, section and paragraph has been carefully crafted to provide a optimal mix of concepts and use cases to be immediately useful for someone using the language professionally.

Why Scala?


@ requests.post(
    "https://api.github.com/repos/lihaoyi/test/issues",
    data = ujson.Obj("title" -> "hello"),
    headers = Map("Authorization" -> s"token $token")
  )
res56: requests.Response = Response(
  "https://api.github.com/repos/lihaoyi/test/issues",
  201,
  "Created",
...

A Compiled Language that feels Dynamic

Python's convenience with Go's performance and scalability, Scala gives you the best of both worlds. Scala's conciseness makes rapid prototyping a joy, while its optimizing compiler and fast JVM runtime provides great performance to support your heaviest production workloads.


@ printHello("1") // wrong type of argument
> cmd128.sc:1: type mismatch;
>  found   : String("1")
>  required: Int
> val res128 = printHello("1")
>                          ^
> Compilation Failed

Easy Safety and Correctness

Tired of fighting TypeErrors and NullPointerExceptions in production? Scala's functional programming style and type-checking compiler helps rule out entire classes of bugs and defects, saving you time and effort you can instead spend developing features for your users.


<dependency>
    <groupId>com.atlassian.commonmark</groupId>
    <artifactId>commonmark</artifactId>
    <version>0.13.1</version>
</dependency>
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

A Broad and Deep Ecosystem

The Scala programming language gives you access to the vast Java ecosystem: runtimes, libraries, profilers, package repositories, all battle-tested and a single install away. No matter what you are building, with Scala you will find everything you need to take your idea to production.

About the Author


Li Haoyi graduated from MIT with a degree in Computer Science and Engineering, and since then has been a major contributor to the Scala community. His open source projects have over 10,000 stars on Github, and are downloaded over 7,000,000 times a month. Haoyi has used Scala professionally to build distributed backend systems, programming languages, high-performance web applications, and much more.

Haoyi writes a blog about Scala and other technical topics at www.lihaoyi.com

Table of Contents


G cluster_1 Part I: Introduction to Scala cluster_2 Part II: Local Development cluster_3 Part III: Web Services cluster_4 Part IV: Program Design p1 Chapter 1: Hands-on Scala                    Chapter 2: Setting Up                     Chapter 3: Basic Scala                    Chapter 4: Scala Collections                Chapter 5: Notable Scala Features 6 Chapter 6 Implementing Algorithms in Scala p1->6 7 Chapter 7 Files and Subprocesses 6->7 11 Chapter 11 Scraping Websites 6->11 19 Chapter 19 Parsing Structured Text 6->19 8 Chapter 8 JSON and Binary Serialization 7->8 9 Chapter 9 Self-Contained Scala Scripts 7->9 12 Chapter 12 Working with HTTP APIs 8->12 14 Chapter 14 Simple Web and API Servers 8->14 16 Chapter 16 Message-based Parallelism with Actors 8->16 13 Chapter 13 Fork-Join Parallelism with Futures 12->13 15 Chapter 15 Querying SQL Databases 14->15 17 Chapter 17 Multi-Process Applications 16->17 18 Chapter 18 Building a Real-time File Synchronizer 17->18 10 Chapter 10 Static Build Pipelines 9->10 20 Chapter 20 Implementing a Programming Language 19->20

Chapter Organization

The chapters of Hands-on Scala are broken down into four parts:

Each part of the book focuses on one particular aspect of using the Scala language. The chapters within each part build up to one or more projects that make use of what you learned throughout the preceding chapters.

The diagram on the left illustrates how the chapters are organized: which chapters depend on each other as prerequisites, and which chapters are independent. You can use this to chart your own path through Hands-on Scala, focusing your attention on the chapters that cover topics you are most interested in.

Free Chapters: Introduction to Scala


G myList myList 1 1 myList->1 2 2 1->2 3 3 2->3 4 4 3->4 5 5 4->5 Nil Nil 5->Nil myOtherList myOtherList 0 0 myOtherList->0 0->1 myThirdList myThirdList -1 -1 myThirdList->-1 -1->1 myTail myTail myTail->2
@ val myList = List(1, 2, 3, 4, 5)
myList: List[Int] = List(1, 2, 3, 4, 5)

@ val myTail = myList.tail
myTail: List[Int] = List(2, 3, 4, 5)

@ val myOtherList = 0 :: myList
myOtherList: List[Int] = List(0, 1, 2, 3, 4, 5)

@ val myThirdList = -1 :: myList
myThirdList: List[Int] = List(-1, 1, 2, 3, 4, 5)

The first 5 chapters of Hands-on Scala are free to read, as a standalone introduction to the Scala language. This is an excellent way to learn the basics of the Scala language whether or not you are intending on purchasing the book.

Introduction to Scala is available both online and as a PDF download. It takes you through setting up, basic language constructs, the Scala standard collections library, and finally lets you use Scala to implement some fun, well-known algorithms. If you are curious about the Scala language or curious about the kind of content that is in this book, feel free to start reading!

Free Online Materials

Hands-on Scala has a free online repository of supporting materials, including more than self-contained 120 executable code examples. This repository forms a great reference for you to quickly look up examples of working code to accomplish common tasks in Scala.

These examples cover everything from basic Scala syntax and standard library APIs, to simple filesystem operations, database access, HTTP servers and clients, programming language interpreters, and distributed file synchronizers. Each example is self-contained, and can be run and tested using the commands provided in its readme.md. These form a great reference cookbook for anyone using Scala, whether or not they read the book, although following along with Hands-on Scala will give you the best experience and help you get the most out of them.

Hands-on Scala ProgrammingCopyright (c) 2020 Li Haoyi (haoyi.sg@gmail.com)ISBN 978-981-14-5693-0First edition published June 1 2020