Table of Contents


Foreword

9

I Introduction to Scala

13
1 Hands-on Scala15
2 Setting Up25
3 Basic Scala39
4 Scala Collections59
5 Notable Scala Features81

II Local Development

105
6 Implementing Algorithms in Scala107
7 Files and Subprocesses127
8 JSON and Binary Data Serialization147
9 Self-Contained Scala Scripts163
10 Static Build Pipelines179

III Web Services

201
11 Scraping Websites203
12 Working with HTTP APIs219
13 Fork-Join Parallelism with Futures237
14 Simple Web and API Servers259
15 Querying SQL Databases285

IV Program Design

305
16 Message-based Parallelism with Actors307
17 Multi-Process Applications327
18 Building a Real-time File Synchronizer345
19 Parsing Structured Text363
20 Implementing a Programming Language385

Conclusion

409

Part I Introduction to Scala


1 Hands-on Scala15
1.1 Why Scala?16
1.2 Why This Book?17
1.3 How This Book Is Organized18
1.4 Code Snippet and Examples20
1.5 Online Materials22
2 Setting Up25
2.1 Windows Setup (Optional)26
2.2 Installing Java26
2.3 Installing Ammonite27
2.4 Installing Mill31
2.5 IDE Support33
3 Basic Scala39
3.1 Values40
3.2 Loops, Conditionals, Comprehensions47
3.3 Methods and Functions51
3.4 Classes and Traits54
4 Scala Collections59
4.1 Operations60
4.2 Immutable Collections66
4.3 Mutable Collections72
4.4 Common Interfaces76
5 Notable Scala Features81
5.1 Case Classes and Sealed Traits82
5.2 Pattern Matching84
5.3 By-Name Parameters90
5.4 Implicit Parameters92
5.5 Typeclass Inference95

Part II Local Development


6 Implementing Algorithms in Scala107
6.1 Merge Sort108
6.2 Prefix Tries112
6.3 Breadth First Search118
6.4 Shortest Paths121
7 Files and Subprocesses127
7.1 Paths128
7.2 Filesystem Operations130
7.3 Folder Syncing134
7.4 Simple Subprocess Invocations138
7.5 Interactive and Streaming Subprocesses142
8 JSON and Binary Data Serialization147
8.1 Manipulating JSON148
8.2 JSON Serialization of Scala Data Types151
8.3 Writing your own Generic Serialization Methods155
8.4 Binary Serialization158
9 Self-Contained Scala Scripts163
9.1 Reading Files Off Disk164
9.2 Rendering HTML with Scalatags165
9.3 Rendering Markdown with Commonmark-Java167
9.4 Links and Bootstrap171
9.5 Optionally Deploying the Static Site175
10 Static Build Pipelines179
10.1 Mill Build Pipelines180
10.2 Mill Modules184
10.3 Revisiting our Static Site Script188
10.4 Conversion to a Mill Build Pipeline189
10.5 Extending our Static Site Pipeline193

Part III Web Services


11 Scraping Websites203
11.1 Scraping Wikipedia204
11.2 MDN Web Documentation208
11.3 Scraping MDN210
11.4 Putting it Together214
12 Working with HTTP APIs219
12.1 The Task: Github Issue Migrator220
12.2 Creating Issues and Comments222
12.3 Fetching Issues and Comments224
12.4 Migrating Issues and Comments229
13 Fork-Join Parallelism with Futures237
13.1 Parallel Computation using Futures238
13.2 N-Ways Parallelism241
13.3 Parallel Web Crawling244
13.4 Asynchronous Futures249
13.5 Asynchronous Web Crawling253
14 Simple Web and API Servers259
14.1 A Minimal Webserver260
14.2 Serving HTML264
14.3 Forms and Dynamic Data266
14.4 Dynamic Page Updates via API Requests273
14.5 Real-time Updates with Websockets277
15 Querying SQL Databases285
15.1 Setting up Quill and PostgreSQL286
15.2 Mapping Tables to Case Classes288
15.3 Querying and Updating Data291
15.4 Transactions296
15.5 A Database-Backed Chat Website298

Part IV Program Design


16 Message-based Parallelism with Actors307
16.1 Castor Actors308
16.2 Actor-based Background Uploads309
16.3 Concurrent Logging Pipelines315
16.4 Debugging Actors322
17 Multi-Process Applications327
17.1 Two-Process Build Setup328
17.2 Remote Procedure Calls331
17.3 The Agent Process333
17.4 The Sync Process335
17.5 Pipelined Syncing338
18 Building a Real-time File Synchronizer345
18.1 Watching for Changes346
18.2 Real-time Syncing with Actors347
18.3 Testing the Syncer354
18.4 Pipelined Real-time Syncing356
18.5 Testing the Pipelined Syncer359
19 Parsing Structured Text363
19.1 Simple Parsers364
19.2 Parsing Structured Values369
19.3 Implementing a Calculator373
19.4 Parser Debugging and Error Reporting378
20 Implementing a Programming Language385
20.1 Interpreting Jsonnet386
20.2 Jsonnet Language Features386
20.3 Parsing Jsonnet388
20.4 Evaluating the Syntax Tree397
20.5 Serializing to JSON403

Hands-on Scala Programming


AuthorLi Haoyi
Published1 June 2020
Websitewww.handsonscala.com
ISBN978-981-14-5693-0

Hands-on Scala Programming Copyright (c) 2020 Li Haoyi (haoyi.sg@gmail.com)

First Edition, published June 1 2020

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the Author.

ISBN 978-981-14-5693-0

Written and Published by Li Haoyi

Book Website: https://www.handsonscala.com/

Chapter Discussion: https://www.handsonscala.com/discuss

Live Chat: https://www.handsonscala.com/chat

Online materials: https://github.com/handsonscala/handsonscala

For inquiries on distribution, translations, or bulk sales, please contact the author directly at haoyi.sg@gmail.com.

The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, the Author shall not have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.

Reviewers

Thanks to all the reviewers who helped review portions of this book and provide the feedback that helped refine this book and make it what it is today.

In alphabetical order:

Alex Allain, Alwyn Tan, Bryan Jadot, Chan Ying Hao, Choo Yang, Dean Wampler, Dimitar Simeonov, Eric Marion, Grace Tang, Guido Van Rossum, Jez Ng, Karan Malik, Liang Yuan Ruo, Mao Ting, Martin MacKerel, Martin Odersky, Michael Wu, Olafur Pall Geirsson, Ong Ming Yang, Pathikrit Bhowmick