Skip to product information
Programming in Scala 4/e
Programming in Scala 4/e
Description
Book Introduction
Scala is a language that combines several functional and object-oriented techniques to create a concise yet expressive language comparable to dynamic languages ​​like Ruby or Python.
Scala is widely used in cutting-edge fields such as big data and machine learning.
Written by Martin Odersky, the creator of Scala, this book is the bible of the Scala language, perfectly explaining various aspects of Scala.

This book covers the basic syntax of Scala, including functions, traits, implicit conversions, and modules, as well as how to use collections, the internal structure of collections, and how to utilize object-oriented and functional programming.
Additionally, the second edition's content, which covers concurrent programming using futures, combinations with Java, parsing, and applications such as GUI programming, has been revised and supplemented, and content on string interpolation, wildcard types, and the revised collection framework has been added to match the latest version.
By following along step by step, you can become a skilled Scala developer, and at the same time, you can learn how to naturally integrate various programming paradigms into one language and how to utilize each one appropriately.
  • You can preview some of the book's contents.
    Preview

index
Chapter 1.
Extensible language
1.1 The language that grows in your mind
1.2 Why Scala is Extensible
1.3 Why Scala?
1.4 Roots of Scala
1.5 Conclusion


Chapter 2.
First Steps in Scala
2.1 Step 1: Learn how to use the Scala interpreter
2.2 Step 2: Let's define variables
2.3 Step 3: Let's define a function
2.4 Step 4: Let's write a Scala script
2.5 Step 5: Let's loop with while and make a decision with if.
2.6 Step 6: Let's iterate using foreach and for.
2.7 Conclusion


Chapter 3.
Scala's second step
3.1 Step 7: Let's specify a type parameter for the array.
3.2 Step 8: Let's use a list
3.3 Step 9: Let's use tuples
3.4 Step 10: Let's use sets and maps.
3.5 Step 11: Learn to recognize functional styles
3.6 Step 12: Read the contents of the file line by line
3.7 Conclusion


Chapter 4.
Classes and Objects
4.1 Classes, fields, and methods
4.2 Semicolon Inference
4.3 Singleton Objects
4.4 Scala Applications
4.5 App Traits
4.6 Conclusion


Chapter 5.
Basic types and operations
5.1 Basic Types
5.2 Literal
5.3 String Interpolation
5.4 Operators are methods
5.5 Arithmetic Operations
5.6 Relational and Logical Operations
5.7 Bit Operations
5.8 Object Identity
5.9 Operator Precedence and Associativity
5.10 Rich Rapper
5.11 Conclusion


Chapter 6.
functional objects
6.1 Fraction Class Specification
6.2 Rational Creation
6.3 Reimplementing the toString method
6.4 Check prerequisites
6.5 Added fields
6.6 Self-reference
6.7 Auxiliary Constructors
6.8 Private Fields and Methods
6.9 Operator Definitions
6.10 Identifiers in Scala
6.11 Method Overloading
6.12 Implicit Type Conversion
6.13 Precautions
6.14 Conclusion


Chapter 7.
Built-in control statements
7.1 if expressions
7.2 while loop
7.3 for expressions
7.4 Handling exceptions with try expressions
7.5 match expression
7.6 Living without break and continue statements
7.7 Variable Scope
7.8 Refactoring Imperative Style Code
7.9 Conclusion


Chapter 8.
Functions and closures
8.1 Method
8.2 Local functions
8.3 First-class hierarchical functions
8.4 Simple form of function literal
8.5 Positional Indicator Grammar
8.6 Partially applied function
8.7 Closure
8.8 Special forms of function calls
8.9 Tail Recursion
8.10 Conclusion


Chapter 9.
Flow control abstraction
9.1 Reducing Code Duplication
9.2 Simplifying Client Code
9.3 Curling
9.4 Writing a New Control Structure
9.5 Calling parameters by name
9.6 Conclusion


Chapter 10.
Inheritance and composition
10.1 2D Layout Library
10.2 Abstract Classes
10.3 Defining a method without parameters
10.4 Class Extensions
10.5 Method and Field Overrides
10.6 Defining parameter fields
10.7 Calling the superclass constructor
10.8 Using the override modifier
10.9 Polymorphism and Dynamic Binding
10.10 Final Member Declaration
10.11 Using Inheritance and Composition
10.12 Above, beside, toString implementation
10.13 Defining a Factory Object
10.14 Height and Width Adjustment
10.15 Gather and test
10.16 Conclusion


Chapter 11.
Scala's hierarchy
11.1 Scala's class hierarchy
11.2 How did you implement multiple base classes?
11.3 Type on the floor
11.4 Defining Your Own Value Class
11.5 Conclusion


Chapter 12.
trait
12.1 How Traits Work
12.2 Simple and Rich Interface
12.3 Example: Rectangle Object
12.4 Ordered Traits
12.5 Stacking Changes Using Traits
12.6 Why is multiple inheritance not allowed?
12.7 To Trait or Not to Trait, That Is the Question
12.8 Conclusion


Chapter 13.
Packages and Imports
13.1 Writing Code Inside a Package
13.2 Concise Access to Related Code
13.3 Import
13.4 Implicit Imports
13.5 Access modifiers
13.6 Package Objects
13.7 Conclusion


Chapter 14.
Assertions and Tests
14.1 Assertive statements
14.2 Testing in Scala
14.3 Failure reporting that provides sufficient information
14.4 Testing with Specifications
14.5 Property-Based Testing
14.6 Test Organization and Execution
14.7 Conclusion


Chapter 15.
Case classes and pattern matching
15.1 A simple example
15.2 Types of Patterns
15.3 Pattern Guard
15.4 Pattern Overlapping
15.5 Sealed Class
15.6 Option Type
15.7 Patterns are everywhere
15.8 Complex Examples
15.9 Conclusion


Chapter 16 List
16.1 List Literals
16.2 List Type
16.3 Creating a list
16.4 Basic List Operations
16.5 List Patterns
16.6 Primary method of the List class
16.7 Higher-order methods of the List class
16.8 Methods of the List Object
16.9 Processing Multiple Lists Together
16.10 Understanding Scala's Type Inference Algorithm
16.11 Conclusion


Chapter 17.
Collection
17.1 Sequence
17.2 Sets and Maps
17.3 Mutable and Immutable Collections
17.4 Collection Initialization
17.5 Tuples
17.6 Conclusion


Chapter 18.
mutable objects
18.1 What makes an object mutable?
18.2 Reassignable Variables and Properties
18.3 Case Study: Discrete Event Simulation
18.4 Language for Digital Circuits
18.5 Simulation API
18.6 Circuit Simulation
18.7 Conclusion


Chapter 19.
Type parameterization
19.1 Functional Queues
19.2 Information Hiding
19.3 Modified notation
19.4 Checking the mutated notation
19.5 Lower bound
19.6 Anticommutation
19.7 Private data of objects
19.8 Upper Bound
19.9 Conclusion


Chapter 20.
abstract member
20.1 A brief review of abstract members
20.2 Type Member
20.3 Abstract val
20.4 Abstract var
20.5 Abstract val initialization
20.6 Abstract Types
20.7 Path-dependent types
20.8 Subdivided Type
20.9 Enumerations
20.10 Case Study: Currency Conversion
20.11 Conclusion


Chapter 21.
Implicit conversions and implicit parameters
21.1 Implicit Conversion
21.2 Rules of Implication
21.3 Implicit conversion to expected type
21.4 Converting the Call Target Object
21.5 Implicit Parameters
21.6 Context Bounds
21.7 When using multiple transformations
21.8 Implicit Debugging
21.9 Conclusion


Chapter 22.
List implementation
22.1 List Class Overview
22.2 ListBuffer Class
22.3 Actual List Class
22.4 Functional when viewed from the outside
22.5 Conclusion


Chapter 23.
Reviewing the for expression
23.1 for expressions
23.2 n Queen's Problem
23.3 Querying with the for formula
23.4 for expression conversion
23.5 Reverse application
23.6 for generalization
23.7 Conclusion
A Closer Look at Chapter 24 Collection 577
24.1 Mutable and Immutable Collections
24.2 Collection Consistency
24.3 Iterable Trait
24.4 Sequence Traits: Seq, IndexedSeq, LinearSeq
24.5 set
24.6 map
24.7 Immutable concrete collection classes
24.8 Mutable concrete collection classes
24.9 array
24.10 String
24.11 Performance Characteristics
24.12 Identity
24.13 views
24.14 Iterator
24.15 Creating Your First Collection
24.16 Converting Java and Scala Collections
24.17 Conclusion
Chapter 25: Scala Collection Architecture 647
25.1 Bundling Common Operations
25.2 Adding a New Collection
25.3 Conclusion


Chapter 26.
extractor
26.1 Example: Extracting Email Addresses
26.2 Extractor
26.3 Patterns with no or only one variable
26.4 Variable argument extractor
26.5 Extractors and Sequence Patterns
26.6 Extractor and Case Classes
26.7 Regular Expressions
26.8 Conclusion


Chapter 27.
Annotation
27.1 Why are annotations needed?
27.2 Annotation Grammar
27.3 Standard Annotations
27.4 Conclusion


Chapter 28. Working with XML
28.1 Semi-structured data
28.2 XML Overview
28.3 XML Literals
28.4 Serialization
28.5 XML Parsing
28.6 Deserialization
28.7 Saving and Loading
28.8 Pattern Matching for XML
28.9 Conclusion


Chapter 29.
Modular programming using objects
Problem 29.1
29.2 Recipe Application
29.3 Abstraction
29.4 Separating modules into traits
29.5 Runtime Linking
29.6 Tracking Module Instances
29.7 Conclusion


Chapter 30.
identity of objects
30.1 Identity in Scala
30.2 Writing an identity comparison method
30.3 Defining identity of parameterized types
30.4 Equals and HashCode Recipes
30.5 Conclusion


Chapter 31.
Combining Scala and Java
31.1 Using Scala in Java
31.2 Annotations
31.3 Wildcard Types
31.4 Compiling Scala and Java Together
31.5 Java 8 Integration
31.6 Conclusion


Chapter 32.
Future and Simultaneity
32.1 Trouble in Paradise
32.2 Asynchronous Execution and Try
32.3 Using Future
32.4 Future Test
32.5 Conclusion


Chapter 33.
Combinator parsing
33.1 Example: Arithmetic Expressions
33.2 Running the parser
33.3 Basic Regular Expression Parser
33.4 Another example: JSON
33.5 Parser results
33.6 Combinator Parser Implementation
33.7 String Literals and Regular Expressions
33.8 Lexical Analysis and Parsing
33.9 Error Report
33.10 Backtracking and LL(1)
33.11 Conclusion


Chapter 34. GUI Programming
34.1 First Swing Application
34.2 Panels and Layouts
34.3 Event Handling
34.4 Example: Celsius/Fahrenheit Converter
34.5 Conclusion


Chapter 35. SCells Spreadsheet
35.1 Screen Framework
35.2 Separating data entry and screen display
35.3 food
Parsing of the 35.4 type
35.5 calculation
35.6 Operation Library
35.7 Change Propagation
35.8 Conclusion


Appendix A.
Using Scala scripts on Unix and Windows
Appendix B.
Glossary of Terms
Appendix C.
References

Publisher's Review
★ Recommended Article ★

When I started programming in 2004 and chose Scala, a then-little-known language, I had no idea what a voyage of exploration and discovery I was embarking on.


When I first started experimenting with Scala, it felt a lot like any other language I'd used before, but it was a process of trial and error, discovery through experimentation, and enlightenment after misunderstanding.
At the time, there was little documentation available to learn Scala, no tutorials, no blogs, and no Scala users experienced enough to teach me.
Of course, there was no book like 『Programming in Scala』.
There were languages ​​that offered amazing new features, but no one really knew how to use them properly.
That situation was both inspiring and disconcerting!

As a Java developer, I had an idea of ​​the programming style, but the experience of coding in Scala every day felt different.
I remember my early adventures learning how to use Scala and working on medium-sized projects with others.
There were times when I decided to refactor (and I had to do this quite often as I continued to discover and learn new features and patterns), and there were times when I had to compile multiple times.

The compiler spits out various errors each time, along with source code line numbers.
Each time, I would look at the code, figure out what was wrong, and then modify the code to fix the error (of course, often the error moved to a different location as I modified the code).
But at each of these steps, the compiler led me right to the root of the problem.
Sometimes I've spent days trying to modify the code without ever successfully compiling it.
But I was able to get my heavily refactored software up and running for the first time, reducing the number of compile errors from a few hundred to a dozen, then reducing those errors to one, and finally eliminating them altogether (meaning it compiled successfully).

And contrary to my expectations, the compiled code ran fine from the start.
As a young programmer who had only experienced Java, Perl, Pascal, Basic, PHP, and JavaScript, I can't express in words how impressive this experience was.
In his keynote at the Scala World conference I organized in 2015, Runar Bjarnason argued that “constraints give freedom, and freedom imposes constraints.”
This truth is most evident when compiling to Scala.
The main purpose of scalac enforcing various constraints is to prevent programmers from making errors that are expected to occur at runtime (and these are the worst of all errors), which frees up all programmers.
This allows programmers to experiment, explore, and change the software with confidence, even without a comprehensive test suite.

Years have passed since then, and to this day I continue to learn Scala, discovering new possibilities, subtle semantic differences, and interesting interactions between different features.
I don't know of any other programming language that offers the depth to keep users engaged for so many years.

Scala is about to undergo a period of tremendous change.
The next major version, Scala 3, will contain as many changes as when I moved from Java to Scala 2 15 years ago.
While the day-to-day programming experience with Scala will remain largely the same, Scala 3 brings a repertoire of new features that span nearly the entire Scala language.
As of this writing, Scala 3 is still a few years away from widespread adoption, and Scala 2 will likely remain the de facto standard for the foreseeable future.

This book covers extensively the latest release of Scala 2, Scala 2.13.
We also introduce the collections library, which has been redesigned and introduced into the standard library to further simplify its structure while incorporating lessons learned after the major redesign introduced in Scala 2.8.
The new collections library can be compiled in both Scala 2 and 3.
So this collection library will provide the foundation for the code that will be used for the next 10 years.
So, before you get excited about the new Scala, it's a great time to pick up this book and start learning!

Jon Pretty
Krakow, Poland, September 12, 2019


★ Target audience for this book ★

The primary target audience is programmers who want to learn how to write programs in Scala.
If you want to tackle your next project in Scala, this book is for you.
Programmers who want to broaden their horizons by learning new concepts will also find this book enjoyable.
For example, a Java programmer can learn various functional programming concepts and more advanced object-oriented ideas by reading this book.
I believe that after learning Scala and its ideas, you will definitely become a better programmer.

We assume that the reader has general programming knowledge.
Although Scala itself is a good language to start learning programming, this book does not teach you how to program.
However, no prior knowledge of a programming language is required.
Although most programmers use Scala on the Java platform, we do not assume readers have any knowledge of Java.
However, since we expect many readers to be familiar with Java, we will occasionally compare Java and Scala to help readers familiar with Java development understand.


★ Structure of this book ★

Chapter 1, 'Extensible Language', explains the design of Scala and its rationale, and provides background history.
Chapter 2, "First Steps in Scala," shows how to handle basic programming tasks in Scala.
I won't go into detail about why each one works.
The goal of Chapter 2 is to get the reader typing and running Scala code.
Chapter 3, "Scala: Second Steps," demonstrates more basic programming tasks to help you get up to speed with Scala more quickly.
After completing Chapter 3, you will be able to use Scala for simple scripting tasks.
Chapter 4, "Classes and Objects," details Scala's basic object-oriented building blocks and shows how to compile and run Scala applications.
Chapter 5, 'Basic Types and Operations', explains Scala's basic types and their literals.
And it explains the operations, precedence, and associativity rules that can be used for each type.
Finally, let's talk about the rich wrapper.
Chapter 6, 'Functional Objects', delves deeper into the object-oriented aspects of Scala.
Let's use an immutable functional rational number as an example.
Chapter 7, 'Built-in Control Statements' shows how to utilize the control structures provided by Scala: if, while, for, try, and match.
Chapter 8, "Functions and Closures," details functions, the basic building blocks of functional languages.
Chapter 9, "Control Flow Abstractions," explains how readers can create their own control abstractions to complement Scala's basic control structures.
Chapter 10, "Inheritance and Composition," discusses how Scala supports object-oriented programming.
The topics covered are not as fundamental as those in Chapter 4, but they are more frequently encountered in practice.
Chapter 11, "Scala's Hierarchy," explains Scala's inheritance hierarchy and the common methods and floor types available in all hierarchies.
Chapter 12, 'Traits', covers the mechanism of combining mixins in Scala.
Chapter 12 shows how traits work, describes common use cases, and shows how traits enhance traditional multiple inheritance.
Chapter 13, "Packages and Imports," discusses problems that arise in large-scale programming.
Describes top-level packages, import commands, and access control modifiers such as protected and private.
Chapter 14, "Assertions and Testing," covers Scala's assertion mechanism and briefly explores several testing tools available in Scala.
In particular, we will focus on ScalaTest.
Chapter 15, "Case Classes and Pattern Matching," introduces a pair of constructs that support writing common, non-encapsulated data structures.
In particular, case classes and pattern matches are useful when creating recursive data such as tree structures.
Chapter 16, 'Lists', details lists, the most commonly used data structure in Scala programs.
Chapter 17, 'Collections', shows how to use basic Scala collections such as lists, arrays, tuples, sets, and maps.
Chapter 18, 'Mutable Objects', explains mutable objects and learns how to express them in Scala.
The latter part of Chapter 18 covers discrete event simulation, which makes practical use of mutable objects.
Chapter 19, 'Type Parameterization', explains some of the information hiding techniques introduced in Chapter 13 with specific examples.
An example is to create a fully functional queue class.
Chapter 19 explains the variance of type parameters and discusses the relationship between variance and information hiding.
Chapter 20, "Abstract Members," describes all the abstract members supported by Scala.
Not only methods, but also fields and types can be defined as abstract members.
Chapter 21, "Implicit Conversions and Implicit Parameters," introduces two elements that help the compiler fill in the necessary information for programmers by allowing them to omit potentially tedious parts of their source code.
Chapter 22, 'List Implementation' explains the implementation of the List class.
It is important to understand how Scala lists work.
Furthermore, this implementation also demonstrates how to leverage some of Scala's features.
Chapter 23, "Revisiting the for Expression," shows how the for expression can be converted into commands that call map, flatMap, filter, foreach, and so on.
Chapter 24, "A Closer Look at Collections," provides a detailed explanation of the Scala collections library.
Chapter 25, "The Architecture of Scala Collections," shows how we built the collections library and explains how you can implement your own collections.
Chapter 26, "Extractor," shows how to pattern match against arbitrary classes, not just case classes.
Chapter 27, 'Annotations', shows how to use language extensions through annotations.
We'll cover standard annotations and how to create your own.
Chapter 28, "Working with XML," explains how to work with XML in Scala. It demonstrates several idioms for generating XML, parsing it, and processing the parsed XML.
Chapter 29, 'Modular Programming with Objects', shows how to use Scala's objects as a module system.
Chapter 30, 'Object Identity', explains what to consider when writing an equals method.
There are a few pitfalls to avoid.
Chapter 31, 'Combining Scala and Java' discusses problems that arise when using Scala and Java together in a single project and proposes solutions.
Chapter 32, 'Futures and Concurrency', shows how to use Futures in Scala.
While you can leverage Java's concurrency primitives and libraries in your Scala programs, using futures helps you avoid deadlocks and race conditions that often plague traditional "threads and locks" approaches.
Chapter 33, "Combinator Parsing," shows how to build a parser using Scala's parser combinator library.
Chapter 34, 'GUI Programming', shows the process of simple Swing GUI programming using the Scala library.
Chapter 35, 'SCells Spreadsheet', brings together what you've learned so far to create a complete spreadsheet application in Scala.


★ Author's Note ★

Because this book is a tutorial, it is recommended that you read it sequentially from beginning to end.
We have made every effort to introduce only one topic at a time, and to explain new topics based on what has been previously described.
So, if you were to flip to the back of a book to get a quick overview of something, you might find it difficult to understand because the topic is probably being explained using concepts you're not familiar with.
If you read each chapter in order, you will be able to easily build your Scala skills one step at a time.
If you come across a term you don't know, check out the 'Lookup' and 'Glossary' sections.
Some readers may just skim through the book.
That's okay though.
The 'Lookup' and 'Glossary' will help you re-examine any overlooked parts.
This book can be used as a language reference even after reading it once.
Although there is a formal specification for the Scala language, the language specification sacrifices readability for correctness.
Although this book doesn't cover every detail of Scala, it covers a broad range of topics and will serve as a handy language reference as you become more proficient in Scala programming.


★ Translator's Note ★

Even in 2014, when I translated the second edition of 『Programming in Scala』, interest in Scala was not that great in Korea.
However, around the same time that the second edition was released, Apache Spark gained popularity and the number of technology startups increased, leading to a rapid increase in interest in languages ​​like Python and Scala that are easier to use than languages ​​like Java.
As a result, the number of developers entering the Scala community has increased, and Scala has received more attention in various study groups and seminars than before.
As Scala versions increase, some features used in older versions are removed, some terms or usage have changed, and some new features are introduced.
Just as everyday languages ​​like Korean and English evolve, the fact that programming languages ​​naturally adopt such changes based on the usage environment or user needs is evidence of a healthy and vibrant language and its community.
I am especially pleased to be able to translate and publish the 4th edition, which has been redesigned to be simpler and has supplemented the contents of the collection library ahead of the release of Scala 3 (Dotty).
As I mentioned in the translator's note for the third edition, using languages ​​of different paradigms can make you a better programmer, whether you're doing procedural or object-oriented programming.
Through this book, I hope that readers will learn Scala well, apply what they have learned in Scala and functional programming, and furthermore, become programmers who can create readable and maintainable code by utilizing the right concepts at the right time, regardless of the programming language they use.

Oh Hyeon-seok
December 2020 in Brisbane, Australia
GOODS SPECIFICS
- Date of issue: January 15, 2021
- Page count, weight, size: 924 pages | 1,760g | 188*235*44mm
- ISBN13: 9791161754871
- ISBN10: 1161754873

You may also like

카테고리