Foreword | 9 |
I Introduction to Scala | 13 |
1 Hands-on Scala | 15 |
2 Setting Up | 25 |
3 Basic Scala | 39 |
4 Scala Collections | 59 |
5 Notable Scala Features | 81 |
II Local Development | 105 |
6 Implementing Algorithms in Scala | 107 |
7 Files and Subprocesses | 127 |
8 JSON and Binary Data Serialization | 147 |
9 Self-Contained Scala Scripts | 163 |
10 Static Build Pipelines | 179 |
III Web Services | 201 |
11 Scraping Websites | 203 |
12 Working with HTTP APIs | 219 |
13 Fork-Join Parallelism with Futures | 237 |
14 Simple Web and API Servers | 259 |
15 Querying SQL Databases | 285 |
IV Program Design | 305 |
16 Message-based Parallelism with Actors | 307 |
17 Multi-Process Applications | 327 |
18 Building a Real-time File Synchronizer | 345 |
19 Parsing Structured Text | 363 |
20 Implementing a Programming Language | 385 |
Conclusion | 409 |
1 Hands-on Scala | 15 |
1.1 Why Scala? | 16 |
1.2 Why This Book? | 17 |
1.3 How This Book Is Organized | 18 |
1.4 Code Snippet and Examples | 20 |
1.5 Online Materials | 22 |
2 Setting Up | 25 |
2.1 Windows Setup (Optional) | 26 |
2.2 Installing Java | 26 |
2.3 Installing Ammonite | 27 |
2.4 Installing Mill | 31 |
2.5 IDE Support | 33 |
3 Basic Scala | 39 |
3.1 Values | 40 |
3.2 Loops, Conditionals, Comprehensions | 47 |
3.3 Methods and Functions | 51 |
3.4 Classes and Traits | 54 |
4 Scala Collections | 59 |
4.1 Operations | 60 |
4.2 Immutable Collections | 66 |
4.3 Mutable Collections | 72 |
4.4 Common Interfaces | 76 |
5 Notable Scala Features | 81 |
5.1 Case Classes and Sealed Traits | 82 |
5.2 Pattern Matching | 84 |
5.3 By-Name Parameters | 90 |
5.4 Implicit Parameters | 92 |
5.5 Typeclass Inference | 95 |
6 Implementing Algorithms in Scala | 107 |
6.1 Merge Sort | 108 |
6.2 Prefix Tries | 112 |
6.3 Breadth First Search | 118 |
6.4 Shortest Paths | 121 |
7 Files and Subprocesses | 127 |
7.1 Paths | 128 |
7.2 Filesystem Operations | 130 |
7.3 Folder Syncing | 134 |
7.4 Simple Subprocess Invocations | 138 |
7.5 Interactive and Streaming Subprocesses | 142 |
8 JSON and Binary Data Serialization | 147 |
8.1 Manipulating JSON | 148 |
8.2 JSON Serialization of Scala Data Types | 151 |
8.3 Writing your own Generic Serialization Methods | 155 |
8.4 Binary Serialization | 158 |
9 Self-Contained Scala Scripts | 163 |
9.1 Reading Files Off Disk | 164 |
9.2 Rendering HTML with Scalatags | 165 |
9.3 Rendering Markdown with Commonmark-Java | 167 |
9.4 Links and Bootstrap | 171 |
9.5 Optionally Deploying the Static Site | 175 |
10 Static Build Pipelines | 179 |
10.1 Mill Build Pipelines | 180 |
10.2 Mill Modules | 184 |
10.3 Revisiting our Static Site Script | 188 |
10.4 Conversion to a Mill Build Pipeline | 189 |
10.5 Extending our Static Site Pipeline | 193 |
11 Scraping Websites | 203 |
11.1 Scraping Wikipedia | 204 |
11.2 MDN Web Documentation | 208 |
11.3 Scraping MDN | 210 |
11.4 Putting it Together | 214 |
12 Working with HTTP APIs | 219 |
12.1 The Task: Github Issue Migrator | 220 |
12.2 Creating Issues and Comments | 222 |
12.3 Fetching Issues and Comments | 224 |
12.4 Migrating Issues and Comments | 229 |
13 Fork-Join Parallelism with Futures | 237 |
13.1 Parallel Computation using Futures | 238 |
13.2 N-Ways Parallelism | 241 |
13.3 Parallel Web Crawling | 244 |
13.4 Asynchronous Futures | 249 |
13.5 Asynchronous Web Crawling | 253 |
14 Simple Web and API Servers | 259 |
14.1 A Minimal Webserver | 260 |
14.2 Serving HTML | 264 |
14.3 Forms and Dynamic Data | 266 |
14.4 Dynamic Page Updates via API Requests | 273 |
14.5 Real-time Updates with Websockets | 277 |
15 Querying SQL Databases | 285 |
15.1 Setting up Quill and PostgreSQL | 286 |
15.2 Mapping Tables to Case Classes | 288 |
15.3 Querying and Updating Data | 291 |
15.4 Transactions | 296 |
15.5 A Database-Backed Chat Website | 298 |
16 Message-based Parallelism with Actors | 307 |
16.1 Castor Actors | 308 |
16.2 Actor-based Background Uploads | 309 |
16.3 Concurrent Logging Pipelines | 315 |
16.4 Debugging Actors | 322 |
17 Multi-Process Applications | 327 |
17.1 Two-Process Build Setup | 328 |
17.2 Remote Procedure Calls | 331 |
17.3 The Agent Process | 333 |
17.4 The Sync Process | 335 |
17.5 Pipelined Syncing | 338 |
18 Building a Real-time File Synchronizer | 345 |
18.1 Watching for Changes | 346 |
18.2 Real-time Syncing with Actors | 347 |
18.3 Testing the Syncer | 354 |
18.4 Pipelined Real-time Syncing | 356 |
18.5 Testing the Pipelined Syncer | 359 |
19 Parsing Structured Text | 363 |
19.1 Simple Parsers | 364 |
19.2 Parsing Structured Values | 369 |
19.3 Implementing a Calculator | 373 |
19.4 Parser Debugging and Error Reporting | 378 |
20 Implementing a Programming Language | 385 |
20.1 Interpreting Jsonnet | 386 |
20.2 Jsonnet Language Features | 386 |
20.3 Parsing Jsonnet | 388 |
20.4 Evaluating the Syntax Tree | 397 |
20.5 Serializing to JSON | 403 |
Author | Li Haoyi |
Published | 1 June 2020 |
Website | www.handsonscala.com |
ISBN | 978-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