Get Programming with Scala [1 ed.] 1617295272, 9781617295270

The perfect starting point for your journey into Scala and functional programming. In Get Programming in Scala you will

1,291 231 7MB

English Pages 560 [562] Year 2021

Report DMCA / Copyright

DOWNLOAD FILE

Get Programming with Scala [1 ed.]
 1617295272, 9781617295270

  • Commentary
  • Vector PDF

Table of contents :
Get Programming with Scala
Contents
Preface
Acknowledgments
About this book
Who should read this book
How this book is organized: A roadmap
About the code
liveBook discussion forum
About the author
Why Scala?
UNIT 0—Hello Scala!
LESSON 1—Why Scala?
1.1 Why Scala?
1.2 Scala and the JVM
1.3 Scala’s key features
Summary
The Scala environment
LESSON 2—The Scala environment
2.1 The REPL installation
2.2 The REPL commands
2.3 The REPL code evaluation
2.4 Other development tools
2.4.1 Git installation
2.4.2 Docker installation
Summary
Scala Build Tool (sbt)
LESSON 3—Scala Build Tool (sbt)
3.1 Why sbt?
3.2 sbt installation
3.3 sbt commands
3.4 Your first sbt project
3.5 sbt project structure
Summary
Values and variables
UNIT 1—The Basics
LESSON 4—Values and variables
4.1 Values
4.2 Variables
Summary
Answers to quick checks
LESSON 5—Conditional constructs and loops
5.1 If-else construct
5.2 While loop
5.3 For loop
Summary
Answers to quick checks
Function as the most fundamental block of code
LESSON 6—Function as the most fundamental block of code
6.1 Functions
Summary
Answers to quick checks
Classes and subclasses to represent the world
LESSON 7—Classes and subclasses to represent the world
7.1 Class
7.2 Subclass
7.3 Abstract class
Summary
Answers to quick checks
The vending machine
LESSON 8—The vending machine
8.1 Setting up the vending machine
8.1.1 The VendingMachine class and its APIs
8.1.2 The vending machine and its operations
8.1.3 Let’s try it out
8.2 Possible improvements to our solution
Summary
Import and create packages
UNIT 2—Object-oriented fundamentals
LESSON 9—Import and create packages
9.1 Import an existing package
9.2 Create a package
Summary
Answers to quick checks
Scope your code with access modifiers
LESSON 10—Scope your code with access modifiers
10.1 Public, the default access modifier
10.2 Private
10.3 Protected
10.4 Which access level to use?
Summary
Answers to quick checks
Singleton objects
LESSON 11—Singleton objects
11.1 Object
11.2 Executable object
11.3 Companion object
11.4 The apply method
Summary
Answers to quick checks
Traits as interfaces
LESSON 12—Traits as interfaces
12.1 Defining traits
12.2 Extending traits
12.3 Sealed traits
12.4 Enumeration in Scala 3
Summary
Answers to quick checks
What time is it?
LESSON 13—What time is it?
13.1 What time is it?
13.1.1 sbt project setup
13.1.2 The business logic layer
13.1.3 The TimeApp executable object
13.1.4 Let’s try it out!
13.2 Possible improvements to our solution
Summary
Pattern matching
UNIT 3—HTTP server
LESSON 14—Pattern matching
14.1 If-else construct vs. pattern matching
14.2 Sealed pattern matching
Summary
Answers to quick checks
Anonymous functions
LESSON 15—Anonymous functions
15.1 Function vs. anonymous function
15.2 Concise notation for anonymous functions
Summary
Answers to quick checks
Partial functions
LESSON 16—Partial functions
16.1 Partial functions
16.1.1 Implementing a partial function
16.1.2 Function composition
16.2 Use case: Exception handling
Summary
Answers to quick checks
HTTP API with http4s
LESSON 17—HTTP API with http4s
17.1 An overview of http4s
17.2 A ping server using http4s
17.2.1 Initial setup
17.2.2 Implementing the API
17.2.3 Building a server
17.2.4 Let’s try it out!
Summary
Answers to quick checks
The time HTTP server
LESSON 18—The time HTTP server
18.1 What time is it?
18.1.1 Setting your sbt project up
18.1.2 The TimePrinter class
18.1.3 The API routes
18.1.4 The HTTP server
18.1.5 Let’s try it out!
18.2 Possible improvements to our solution
Summary
Case classes to structure your data
UNIT 4—Immutable data and structures
LESSON 19—Case classes to structure
your data
19.1 Case class
19.2 Pattern matching and case classes
19.3 Case object
Summary
Answers to quick checks
Higher order functions
LESSON 20—Higher order functions
20.1 Functions as parameters
20.2 Functions as return values
Summary
Answers to quick checks
What is purity?
LESSON 21—What is purity?
21.1 A definition of purity
21.2 Differentiating between pure and impure functions
Summary
Answers to quick checks
Option
LESSON 22—Option
22.1 Why Option?
22.2 Creating an Option
22.3 Pattern matching on Option
Summary
Answers to quick checks
Working with Option: map and flatMap
LESSON 23—Working with Option: map and flatMap
23.1 Transforming an Option
23.1.1 The map function
23.1.2 The flatten function
23.1.3 The flatMap function
Summary
Answers to quick checks
Working with Option: For-comprehension
LESSON 24—Working with Option: For-comprehension
24.1 For-comprehension on Option
24.1.1 For-comprehension as syntactic sugar for nested map and flatMap calls
24.1.2 Filtering values within for-comprehension
24.2 Other operations on Option
Summary
Answers to quick checks
Tuple and unapply
LESSON 25—Tuple and unapply
25.1 Tuples
25.2 Implementing the unapply method
Summary
Answers to quick checks
Rock, Paper, Scissors, Lizard, Spock!
LESSON 26—Rock, Paper, Scissors, Lizard, Spock!
26.1 Implementing Rock, Paper, Scissors, Lizard, Spock!
26.1.1 sbt project setup and packages
26.1.2 Defining a symbol
26.1.3 Representing a player
26.1.4 Defining a game
26.1.5 The API routes
26.1.6 The HTTP server
26.1.7 Let’s try it out!
26.2 Possible improvements to our solution
Summary
List
UNIT 5—List
LESSON 27—List
27.1 Creating a list
27.2 Adding elements to a list
27.3 Pattern matching on a list
Summary
Answers to quick checks
Working with List: map and flatMap
LESSON 28—Working with List: map and flatMap
28.1 The map, flatten, and flatMap operations
28.1.1 The map function
28.1.2 The flatten function
28.1.3 The flatMap function
28.2 For-comprehension
Summary
Answers to quick checks
Working with List: Properties
LESSON 29—Working with List: Properties
29.1 Size of a list
29.2 Properties of the elements in a list
Summary
Answers to quick checks
Working with List: Element selection
LESSON 30—Working with List: Element selection
30.1 Selecting an element by its position
30.2 Finding an element with given features
30.3 Picking the minimum or maximum item
Summary
Answers to quick checks
Working with List: Filtering
LESSON 31—Working with List: Filtering
31.1 Dropping and taking elements
31.2 Filtering Items of a list
31.3 Removing duplicates
Summary
Answers to quick checks
Working with List: Sorting and other operations
LESSON 32—Working with List: Sorting and other operations
32.1 Sorting elements
32.2 Converting a list to a string
32.3 Sum elements of numerical sequences
32.4 Grouping elements by feature
Summary
Answers to quick checks
The movies dataset
LESSON 33—The movies dataset
33.1 Download the base project
33.2 Parsing a movie entity
33.3 Printing query results
33.4 Querying the movie data set
Summary
Set
UNIT 6—Other collections and error handling
LESSON 34—Set
34.1 Creating a set
34.2 Adding and removing elements
34.3 The map, flatten, and flatMap operations
34.3.1 The map function
34.3.2 The flatten function
34.3.3 The flatMap function
34.4 For-comprehension
Summary
Answers to quick checks
Working with Set
LESSON 35—Working with Set
35.1 The Union, Intersection, and Difference operations
35.2 Other operations on Set
Summary
Answers to quick checks
Map
LESSON 36—Map
36.1 Creating Map
36.2 Adding and removing elements
36.3 Merge and remove multiple entries
36.4 The map and flatMap operations
36.4.1 The map function
36.4.2 The flatMap function
36.5 For-comprehension
Summary
36.6 Answers to quick checks
Working with Map
LESSON 37—Working with Map
37.1 Retrieving a value for a given key
37.2 Getting all keys and values
37.3 Other operations on Map
Summary
Answers to quick checks
Either
LESSON 38—Either
38.1 Why Either?
38.2 Creating an Either
38.3 Pattern matching on Either
38.4 The map and flatMap operations
38.4.1 The map function
38.4.2 The flatMap function
38.5 For-comprehension
Summary
Answers to quick checks
Working with Either
LESSON 39—Working with Either
39.1 Retrieving an Either value
39.2 Properties of an Either value
Summary
Answers to quick checks
Error handling with Try
LESSON 40—Error handling with Try
40.1 Creating a Try
40.2 Pattern matching on Try
40.3 The map, flatten, and flatMap operations
40.3.1 The map function
40.3.2 The flatten function
40.3.3 The flatMap function
40.4 For-comprehension
40.5 Other operations on Try
Summary
Answers to quick checks
The library application
LESSON 41—The library application
41.1 Download the base project
41.2 Parsing a book entity
41.3 The business logic layer
41.3.1 Performing a book search
41.3.2 Reserving a book
41.3.3 Returning a book
41.4 Let’s give it a try!
41.5 Possible improvements to our solution
Summary
Implicit and type classes
UNIT 7—Concurrency
LESSON 42—Implicit and type classes
42.1 Implicit parameters and values
42.2 Implicit resolution
42.3 Type classes
Summary
Answers to quick checks
Future
LESSON 43—Future
43.1 Why Future?
43.2 Creating an instance of Future
43.3 Processing Future on completion
Summary
Answers to quick checks
Working with Future: map and flatMap
LESSON 44—Working with Future: map and flatMap
44.1 The map, flatten, and flatMap operations
44.1.1 The map function
44.1.2 The flatten function
44.1.3 The flatMap function
Summary
Answers to quick checks
Working with Future: For-comprehension and other operations
LESSON 45—Working with Future: For-comprehension and other operations
45.1 For-comprehension
45.2 Retrieving the first Future to complete
Summary
Answers to quick checks
Database queries with Quill
LESSON 46—Database queries with Quill
46.1 Project setup
46.1.1 Download the base project
46.1.2 Starting the PostgreSQL server
46.2 Connecting to the PostgreSQL server
46.3 Executing queries
46.4 Running generated queries
Summary
Answers to quick checks
The quiz application: Part 1
LESSON 47—The quiz application: Part 1
47.1 Download the base project
47.2 Health check queries
47.3 Category queries
47.4 Question and answer queries
47.5 Let’s give it a try!
Summary
JSON (de)serialization with circe
UNIT 8—JSON (de)serialization
LESSON 48—JSON (de)serialization with circe
48.1 Project setup
48.2 JSON serialization: From instance to JSON
48.3 JSON deserialization: From JSON to instance
Summary
Answers to quick checks
Lazy evaluation
LESSON 49—Lazy evaluation
49.1 By-name parameters
49.2 Lazy values
Summary
Answers to quick checks
The IO type
LESSON 50—The IO type
50.1 Why IO?
50.2 Project setup
50.3 Synchronous side effect
50.4 Asynchronous side effect
Summary
Answers to quick checks
Working with the IO type
LESSON 51—Working with the IO type
51.1 The map and flatMap operations
51.1.1 The map function
51.1.2 The flatMap function
51.2 For-comprehension
51.3 Parallel execution
Summary
Answers to quick checks
Testing with ScalaTest
LESSON 52—Testing with ScalaTest
52.1 Project setup
52.2 Your first test
52.3 Asynchronous testing
Summary
Answers to quick checks
The quiz application: Part 2
LESSON 53—The quiz application: Part 2
53.1 Download the base project
53.2 Generic endpoints
53.3 Displaying the available categories
53.4 Creating a quiz
53.5 Answering a quiz
53.6 The HTTP server
53.7 Writing tests
53.8 Let’s give it a try!
Summary
Index
Index
Symbols
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
Y

Polecaj historie