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 needed to go from "hello world" to building production applications like interactive websites, parallel web crawlers, and distributed systems in Scala. In the process you will learn how to use the Scala language to solve challenging problems in an elegant and intuitive manner.

Hands-on Scala is available now as an E-Book in PDF, EPub, and Mobi formats, or as a Paperback on Amazon. Currently 50% off until the end of the month!

Buy Hands-on Scala E-Book
Buy Hands-on Scala Paperback
Hands-On Scala is the best way to learn about writing Scala in this simple and straightforward manner, and a great resource for getting things done using the Scala ecosystem.
I helped review some of this book - excellent work by @li_haoyi; if you're looking to get into Scala, recommended!
Fantastic book! I got the privilege to review it, and I can honestly say it made me a better engineer! Tour de force by @li_haoyi
This will be great! @li_haoyi asked me to read an early draft. Very pragmatic. Great examples.

G cluster_0 Main Process cluster_1 Sub Process os.watch os.watch SyncActor SyncActor os.watch->SyncActor 1. ChangedPath Agent Agent SyncActor->Agent 2. StatPath SyncActor->Agent 5. WriteOver HashActor HashActor HashActor->SyncActor 4. StatAndHashInfo                               Agent->HashActor 3. StatInfo

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.


Code First

Hands-on Scala starts and ends with working code. The concepts you learn in this book are backed up by over 120 executable code examples that demonstrate the concepts in action, and every chapter ends with a set of exercises with complete executable solutions. More than just a source of knowledge, Hands-on Scala's wide variety of working code examples also serve as a cookbook you can use to kickstart any project you may work on in future.

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 Data 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 10 Chapter 10 Static Build Pipelines 9->10 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 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.

G myList myList 1 1 myList->1 myOtherList myOtherList 0 0 myOtherList->0 myThirdList myThirdList -1 -1 myThirdList->-1 myTail myTail 2 2 myTail->2 1->2 3 3 2->3 4 4 3->4 5 5 4->5 Nil Nil 5->Nil 0->1 -1->1
@ 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)

Free Chapters: Intro to Scala

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 online, or as free PDF, EPub or Mobi downloads. 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 120 self-contained 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.

Learn Together

You don't need to go alone! Hands-on Scala has online discussion threads for every chapter, and a chat room for more interactive discussions. Get help from the author or compare notes with fellow learners, so you never need to get stuck. Hands-on Scala's online community of learners helps enrich your learning experience far beyond that of other books or tutorials.

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