A
abstraction 160
accumulator 125
acquire function 420
actor model 376
ADTs (algebraic data types) 242 , 254 , 259
destructuring ADTs using pattern matching 256
implementing functions with ADT-based parameters 265
using ADT-based models in behaviors 255
advanced list operations 322
algorithms
passing as arguments 75
writing pipeline-based 136
anonymous functions 92 , 94
API calls
dealing with external impure 316
infinite stream of 355
APIs
as easy to use 107
integrating using imperative libraries and IO 407 – 409
appending immutable values 69
arguments
passing algorithms as 75
passing functions as 87
passing state as 33
passing user-defined functions as 80
asynchronicity 391
coding functional asynchronous programs 395
designing functional asynchronous programs 393
preparing for asynchronous access 392
atomic references 377
attempt function 466
avoiding null 167
B
bag of functions 404 , 415
batching
implementation 372
need for 371
behaviors
modeling 263
modeling as data 264
using ADT-based models in 255
BiFunction type 79
BigInt type 443
bottom-up design 321 , 329
bounded context 402
bugs
finding and fixing in implementation 443
wrong test or 439
bulletproof models 231
business domain modeling 402
C
caching 306
cancel function 394
CAS (compare-and-swap) operations 377 , 378
case class 254
chained flatMap 144
chaining method calls 74
checked exceptions 196 – 197 , 201
chooseNum function 440
clean code 37
client code 107
code duplication 79
coding
functional asynchronous programs 395
to interface 403
using Refs and fibers 386 – 387
when code lies 8
coding to the interface 403
combinators 349
Comparator 73
compare-and-set operation 377
compare-and-swap (CAS) operations 377 , 378
compilation errors 206
compile.drain 352
compiler 205 , 208 – 209
compile.toList function 350
composability 189
computations as IO values 278
compute function 375
concurrent programs 365 – 396
asynchronicity 391
coding functional asynchronous programs 395
designing functional asynchronous programs 393
preparing for asynchronous access 392
atomic references 377
batching
implementation 372
need for 371
coding using Refs and fibers 386 – 387
concurrent state 374
concurrent thinking 389 – 390
concurrent world 373
declarative concurrency 367
imperative concurrency 375 – 376
actor model 376
monitors and locks 375
thread-safe data structures 376
IOs that run infinitely 388
making it all concurrent 381
managing fibers manually 394
modeling concurrency 385
parSequence function 382
practicing concurrent IOs 384
Ref values 378
updating 379
using 380
sequential vs. concurrent 368 – 370
threads 366
concurrent shared mutable state 373
conditional recovery 198
using functional style 200
using imperative style 199
configurable retry strategies 307
consumers 344
contains function 247 – 248
copies 25 , 33 , 53
coupling 249
currying 120 – 121 , 414
custom generators 440 – 441
custom-made functions 90
D
data
coming from outside world 178
making sure only valid data combinations are possible 241
passing copies of 25 , 33
possibility of absence in 242
separating real data access concern 406
using IO to store 301 – 305
data access modeling 403
data streams 341
data usage tests 450 – 451 , 464
DDD (domain-driven design) 402
dead ends 330
debugging mutations 55 – 56
declarative concurrency 367
declarative programming 9 – 11 , 89
declarative style 290
def keyword 64
designing functional programs 399 – 425
currying and inversion of control 414
following design 410
functions as values 415
configuring bag of functions 415
passing “querying behavior” 415
handling resources 420
implementing input actions as IO values 411 – 412
connecting to server 411
creating query and executing it 411
parsing results 412
integrating with APIs using imperative libraries and IO 407 – 409
connecting to and querying 408
creating query 407
extracting and parsing query results 409
making it fast 423 – 424
making it right 418
making it work 417
resource leaks 419
designing functional programs ( continued )
separating library IO from other concerns 413
testing and 453
using Resource value 421
domain-driven design (DDD) 402
drop function 322
dropping tuples 324
DRY (don’t repeat yourself) 57 , 257
duplication 257
E
Either
conveying error details using 217 – 218
error handling 225 – 226
Option and 224
practicing safe functions that return 223
refactoring to 219
returning Either instead of Option 220 – 222
working with 227
enumerators 154 , 165 , 250 , 254
error handling 173 – 228 . See also Either
building from small blocks 179 , 188
checked exceptions 196 – 197 , 201
compilation errors 206
compiler 205
conditional recovery 198
using functional style 200
using imperative style 199
dealing with data coming from outside world 178
folding List of Options into Option of List 214
functional design is building from small blocks 189
functions compose even in presence of errors 204
functions, values, and expressions 192
handling gracefully 174 , 185
how to know what failed 216
implementing function that returns Option 186
implementing sorting requirement 177
letting compiler be guide 208 – 209
multiple possible errors 215
Option forcing handling possible errors 187
orElse function 202
parsing List 181
parsing Strings into immutable objects 180
potential errors 183
practicing functional error handling 203
propagation of 194
returning null 184
safe functions that returns Option 190 – 191 , 193
strategies 210 – 212
all-or-nothing strategy 210 – 213
best-effort strategy 210 , 212
transforming List of Options into flat List 207
values represent errors 195
working with Option/Either 227
examples, testing by providing 431 – 433
exists function 246 , 247 – 248
expressions 153 , 192
putting into function body 68
transforming relations into 68
external APIs
integrating IO actions with 271
testing low-level integration with 454
extracting query results 409
F
failures
handling IO failures in streams 356
IO failures 290
practicing failure recovery in IO values 296
running program described by IO may fail 291
toward functional IO with handling 298
vs. errors 298
where to handle potential failures 297
fallbacks 295
fibers 383
coding using 386 – 387
managing manually 394
filter function 102 – 104 , 145 , 158 , 157 – 159 , 246 , 261
FiniteDuration product type 360
flatMap function 139 – 141 , 145 , 158 , 167 , 208 , 246 , 455
chained flatMap 144
changing size of lists and 141
comparing map, foldLeft, and 161
for comprehensions vs. 150 – 151
nested flatMaps 145 , 147 – 148
values that depend on other values 146
flatMap/map sequences 388
flatten function 139 , 208 , 246
foldLeft function 124 – 127 , 161 , 246
forall function 245 , 246 , 247 – 248 , 248
forAll function 437 , 440
for comprehensions 149
checking all combinations using 156
defining 165
flatMaps vs. 150 – 151
inside 154
more sophisticated 155
overview 152 – 153
practicing 164
using with many types 163
for comprehensions ( continued )
using with noncollection types 166
using with Set types 162
foreach function 370
foreverM function 388
FP (functional programming) 4 – 19
business domain modeling 402
dealing with moving parts 61
embracing declarative programming 89
functions 6 – 7
functions, writing 17
how useful is learning 12
imperative vs. declarative 9 – 11
Scala 13
Scala, practicing functions in 14
streams are values in 347
tools needed for 15
when code lies 8
FRP (functional reactive programming) 362
functional APIs
designing 114
iterative design of 115
functional architecture 300
functional atomic reference 378
functional core 300 , 303
functional data design 261 – 262
functional IO type 278
functional jigsaw puzzle 328
functional software design 91
functional stream operations 342
functional Stream type 346
functions 71 – 131 , 192
adding new parameter 108
API as easy to use 107
applying to elements of list 96 – 97
as parameters 81 – 82
as values 111
configuring bag of functions 415
designing functional programs 415
passing querying behavior 415
changing requirements 76
choosing to test 430
composing even in presence of errors 204
currying 120 – 121
delegating work by passing 437
filter function 102 – 104
foldLeft function 124 – 127
functional APIs
designing 114
iterative design of 115
implementing requirements as 72
impure functions and mutable values 73
learning once to use everywhere 100
map function 97 – 98 , 98 – 99
modeling immutable data 128
more concise syntax for inline functions 130
overview 6 – 7
passing 95
as arguments in Scala 87
inline 92
in Scala 84 , 93 – 94
passing user-defined functions as arguments 80
practicing 88
to custom-made functions 90
passing function values 122
problems with reading functional Java 83
public statics 6
putting expressions into 68
recursive 331
reducing many values into single value 123 – 124
repetition 106
returning 109 – 110 , 112 – 113
from returned functions 116 – 117
using flexible API built with 118
returning parts of list
based on condition 101
using filter function 102
signatures 75 , 86
small functions and their responsibilities 91
sortBy function 85
tests are just 429
that lie 52
using Function syntax to deal with code duplication 79
using Java Function values 78
using Java Streams to sort list 74
using multiple parameter lists in 119
using product types with higher-order functions 129
writing 17
Function syntax 79
G
generators 165 , 343
Gen.identifier generator 445
green tests 466
guard expression 165
H
higher-order functions 122 , 245 – 246
I
immutable maps 318 – 319
immutable states 61
immutable values 47 – 70
as fuel for engine 48
building intuition about immutability 63
case for immutability 49 – 50
fighting mutability by working with copies 53
functions that lie 52
in Scala 62
modeling using 401
moving parts
dealing with 60
using FP (functional programming) 61
mutability is dangerous 51 , 54 – 57
parsing Strings into immutable objects 180
practicing immutable slicing and appending 69
questions about 66
shared mutable state 58
purely functional approach to 67 – 68
state’s impact on programming abilities 59
String API 64 – 65
imperative concurrency 375 – 376
actor model 376
monitors and locks 375
thread-safe data structures 376
imperative programming 9 – 11 , 342
imperative Java 96 , 408
IO actions 273 – 274
pure functions 23
sequential programs 138
impure functions 272
difference between pure functions and 30 , 38 – 39
IO actions pushing out 281
mutable values and 73
inheritance 260
inline functions
more concise syntax for 130
passing 92
integer overflow 443
integration tests
local servers as Resources in 455
property-based integration tests 461
speed of 460
writing 458 – 459
Int type 15 , 16
inversion of control 414
IO actions 269 – 312
combining two IO values into single IO value 283
disentangling concerns by working with values only 285
doing IO vs. using IO’s result 276
failures 290
functional architecture 300
handling failures
potential 297
toward functional IO with 298
handling imperatively 277
imperative solution to 273 – 274
implementing fallbacks using orElse and pure 295
implementing recovery strategies using IO.orElse 294
integrating with external API 271
IO type is viral 286
IO values 279
computations as 278
in wild 280
practicing creating and combining 284
practicing failure recovery in 296
orElse 292 – 293
practicing functional signature intuitions 311
properties of 272
pure functions don’t lie 299
pushing impurity out 281
running program described by IO may fail 291
streams and 345
toward functional IO 289
treating everything as values 306
treating retries as values 307 – 308
treating unknown number of API calls as values 309 – 310
using IO to store data 301 – 305
using values fetched from two IO actions 282
working with values 287 – 288
IO calls
how many to make 320
waiting between 360
IO.orElse 294
IO values 279
computations as 278
implementing input actions as 411 – 412
connecting to server 411
creating query and executing it 411
parsing results 412
infinite streams of IO-based values 351
in wild 280
practicing concurrent IOs 384
practicing creating and combining 284
practicing failure recovery in 296
streams of IO-based values 350
that run infinitely 388
isolated integration tests 456
J
Java
problems with reading 83
using Java Function values 78
Java, imperative 96 , 408
Java Streams 74 , 96
JDK (Java Development Kit) 15
L
List
folding List of Options into Option of 214
parsing 181
transforming List of Options into flat List 207
List API 62 , 287
List class 84
list comprehensions 152
List(...) constructor 41
List.empty function 41
lists
dealing with lists of 142 – 143
flatMap and changing size of 141
returning based on condition 101
returning using filter function 102
List type 154 , 246 , 248
lock-free mechanics 377
locks 375
logic
business logic as pure function 405
changes in model force changes in 243
using data modeled as Options in 244
M
maintainability 266 – 267
map function 97 – 98 , 145 , 161 , 246
MapReduce systems 314
maps 326 – 327
Map type 318
mathematical functions 34
modeling data 229 – 268
ADTs (algebraic data types) 254 , 259
destructuring ADTs using pattern matching 256
implementing functions with ADT-based parameters 265
using ADT-based models in behaviors 255
changes in model force changes in logic 243
concurrency 385
coupling concept inside single product type 249
design and maintainability 266 – 267
duplication and DRY 257
finite possibilities 250
forall/exists/contains functions 247 – 248
functional data design 261 – 262
higher-order functions 245 – 246
inheritance 260
making sure only valid data combinations are possible 241
modeling behaviors 263
modeling behaviors as data 264
newtypes 259
practicing 240
protecting against misplaced parameters 238
using 239
pattern matching 258 – 259
possibility of absence in data 242
primitive types
designing with 232
making jobs harder 237
pain of 234 – 235
problems with 236
using data modeled as 233
to minimize programmers’ mistakes 230
using data modeled as Options in logic 244
using sum type + product type combo 253 – 254
using sum types 251 – 252
well-modeled data can’t lie 231
modeling immutable data 128
model module 240
monitors 375
multithreading 368
mutability 51 , 54 – 57
mutable values 73
mutating copies 57
mutating values 53
N
nested flatMaps 145 , 146 , 147 – 148
new keyword 415
newtypes 259
practicing 240
protecting against misplaced parameters 238
using 239
noncollection types 166
Nothing type 388
null 184
O
object definition 238
OOP (object-oriented programming) 60
optimistic locking 377
Option
composing 201
Either and 224
folding List of Options into Option of List 214
forcing to handle possible errors 187
implementing function that returns 186
overview 167
parsing with 169 – 170
safe functions that return 190 – 191 , 193
transforming List of Options into flat List 207
using data modeled as Options in logic 244
using to describe data 241
working with 227
orElse function 202 , 215 , 246 , 292 – 293 , 295
P
parameters
adding new 108
functions as 81 – 82
implementing functions with ADT-based parameters 265
newtypes protecting against misplaced 238
using multiple parameter lists in functions 119
parSequence function 382
parsing
as pipeline 168
List 181
query results 409
results 412
Strings into immutable objects 180
with Option type 169 – 170
passing copies of data 33
passing functions 95
as arguments in Scala 87
as parameters 413
inline 92
in Scala 84 , 93 – 94
practicing 88
programming by passing function values 122
to custom-made functions 90
passing state
as argument 33
focusing on logic by 28
pattern matching
destructuring ADTs using 256
on tuples 325
pipelines
composing larger programs from smaller pieces 137
parsing as 168
writing pipeline-based algorithms 136
practicing exercises 14
prepended function 66
primitive operations 349
primitive types
designing with 232
making jobs harder 237
pain of 234 – 235
problems with 236
using data modeled as 233
producers 344
product types 128
coupling concept inside single 249
with higher-order functions 129
programming languages 35
property-based testing 435 , 444 – 445
failures of 438
generating properties 434
integration tests 461
properties and examples 446
prototyping 330
public statics 6
pure functions 21 – 45
breaking code 24 , 26
by mutating copies 57
clean code and 37
coding imperatively 23
difficulty of 36
don’t lie 299
focusing on logic by passing state 28
impure functions vs. 30 , 38 – 39
in programming languages 35
mutating values inside 53
necessity of 22
passing copies of data 25
practicing pure functions in Scala 41
recalculating instead of storing 27
refactoring to 31 – 33
testing 42 – 44
trusting in 34
using Scala to write 40
without state 29
pure, implementing fallbacks using 295
Q
queries
creating and executing 411
extracting and parsing results 409
passing “querying behavior” 415
R
range function 287
readability 91
recalculating 27 , 32
receive method 376
recipes 290
recovery strategies 294
recursion, base case 333
recursive functions 331
creating different IO programs using 338
dealing with absence in future using 334
infinity 332 , 335 – 337
making arbitrary number of calls 339
problems with recursive version 340
structure 333
recursive values 348
red-green-refactor 465
red tests 467
refactoring
to Either 219
to pure functions 31 – 33
referential transparency feature 57
referential transparency property 37
Refs 378 , 386 – 387
updating 379
using 380
relations
between values 67
transforming into expressions 68
release function 420
repetition 106
REPL (read–evaluate–print loop) 15
getting to know 16
List API 62
requirements coverage 447
resources
handling 420
leaks 419
Resource value 421 , 455
returning functions 109 – 110 , 112 – 113
from returned functions 116 – 117
using flexible API built with 118
S
Scala 13
cheat sheet 471 – 475
functional approach in 61
immutable values in 62
passing functions in 84 , 93 – 94
practicing functions in 14
practicing pure functions in 41
signatures with function parameters in 86
writing pure functions with 40
scalacheck library 435
schedule function 277 , 287
separation of concerns 29 , 403
sequential programs 135 – 171 , 155 , 381
abstraction 160
avoiding null 167
comparing map, foldLeft, and flatMap 161
composing larger programs from smaller pieces 137
concurrent programs vs. 368 – 370
filtering function 157 – 159
flatMap function 139 – 141
chained flatMap 144
nested flatMaps 145 , 147 – 148
values that depend on other values 146
flatMaps vs. for comprehensions 150 – 151
flatten function 139
for comprehensions 149
checking all combinations using 156
defining 165
inside 154
more sophisticated 155
overview 152 – 153
practicing 164
using with many types 163
using with noncollection types 166
using with Set types 162
imperative approach 138
lists
dealing with lists of 142 – 143
flatMap and changing size of 141
Option type 167
parsing as pipeline 168
parsing with Option type 169 – 170
writing pipeline-based algorithms 136
servers
connecting to 411
local servers as Resources in integration tests 455
service integration tests 454
Set types 162
shared mutable state 58 , 373
purely functional approach to 67 – 68
state’s impact on programming abilities 59
short-circuiting 194
side effects
executing for 352
testing side-effectful requirements 448
signatures 75
practicing functional signature intuitions 311
vs. body 7
with function parameters in Scala 86
single retry strategy 294
slicing 69
sliding windows 358 – 359
sortBy function 84 , 85
sorting requirement 177
sort method 73
state 23 , 58
focusing on logic by passing 28
passing as argument 33
pure functions without 29
storing
recalculating instead of 27 , 32
using IO to store data 301 – 305
Stream.metered combinator 360
streams 313 – 363
advanced list operations 322
are recursive values 348
benefits of using stream-based approach 362
bottom-up design 321 , 329
data streams 341
dealing with external impure API calls 316
dealing with unknown number of values 315
executing for side effects 352
functional approach to design 317
functional jigsaw puzzle 328
functional Stream type 346
handling IO failures in 356
how many IO calls to make 320
immutable maps 318 – 319
infinite stream of API calls 355
infinite streams of IO-based values 351
in FP are values 347
in imperative languages 342
IO and 345
of IO-based values 350
pattern matching on tuples 325
practicing stream operations 353
primitive operations and combinators 349
processing 344
prototyping and dead ends 330
recursive functions 331
creating different IO programs using 338
dealing with absence in future using 334
infinity and 335 – 337
infinity and laziness 332
making arbitrary number of calls 339
problems with recursive version 340
structure 333
separated concerns 357
sliding windows 358 – 359
stream processing, producers, and consumers 344
to infinity and beyond 314
tuples 323
using to advantage 354
values on demand 343
waiting between IO calls 360
working with maps and tuples 326 – 327
zipping 361
zipping and dropping 324
String API 64 – 65
Strings
parsing into immutable objects 180
stubbing external services 452
sum types 250 , 254
using 251 – 252
with product type 253 – 254
synchronization primitives 374
syntactic sugar 152
T
test-driven development 463 , 468
test function 429 , 461
testing functional programs 427 – 470
adding more red tests 467
bugs
finding and fixing in implementation 443
wrong test or 439
by providing examples 431 – 433
by providing properties 436
choosing functions to test 430
choosing right testing approach 462
custom generators 440 – 441
data usage tests 450 – 451
delegating work by passing functions 437
design and 453
identifying right test for job 449
integration tests
property-based integration tests 461
speed of 460
writing 458 – 459
testing functional programs ( continued )
integration with service is single responsibility 457
local servers as Resources in integration tests 455
making tests green 466
practicing stubbing external services using IO 452
property-based testing 435 , 444 – 445
failures of 438
generating properties 434
properties and examples 446
pure functions 42 – 44
red-green-refactor 465
requirements coverage 447
service integration tests 454
side-effectful requirements 448
test-driven development 463 , 468
testing complicated scenarios in readable way 442
tests are just functions 429
writing isolated integration tests 456
writing test for feature that doesn’t exist 464
threads 366
thread-safe data structures 376
toList function 207
toLong function 443
tuples 323
pattern matching on 325
working with 326 – 327
zipping and dropping 324
types, using for comprehensions
with many 163
with noncollection types 166
with Set types 162
U
Unit type 272
unsafeRunSync() function 281
updateAndGet 377
updatedWith function 370
V
values 192
dealing with unknown number of 315
disentangling concerns by working with only 285
functions are 111
IO actions
combining two IO values into single IO value 283
using values fetched from two 282
working with 287 – 288
on demand 343
reducing many values into single value 123 – 124
relations between 67
represent errors 195
treating everything as 306
treating retries as 307 – 308
treating unknown number of API calls as 309 – 310
W
writing
functions 17
integration tests 458 – 459
isolated integration tests 456
Y
yield expression 165
Z
zero-cost wrapper. See newtypes
zip function 322
zipping
streams 361
tuples 324