Functional and Concurrent Programming

Core Concepts and Features

Af Michel Charpentier
Detaljer Addison Wesley Professional
ISBN 9780137466542
Sprog Engelsk
Pris 699,00 kr.
Paperback

Ikke på lager

Ikke på lager. Bestil nu og få den leveret om ca. 12 hverdage

Leverage Modern Language Constructs to Write High-Quality Code Faster

The functional and concurrent programming language features supported by modern languages can be challenging, even for experienced developers. These features may appear intimidating to OOP programmers because of a misunderstanding of how they work. Programmers first need to become familiar with the abstract concepts that underlie these powerful features.

In Functional and Concurrent Programming, Michel Charpentier introduces a core set of programming language constructs that will help you be productive in a variety of programming languages--now and in the future. Charpentier illustrates key concepts with numerous small, focused code examples, written in Scala, and with case studies that provide a thorough grounding in functional and concurrent programming skills. These skills will carry from language to language--including the most recent incarnations of Java. Using these features will enable developers and programmers to write high-quality code that is easier to understand, debug, optimize, and evolve.

Key topics covered include:

  • Recursion and tail recursion
  • Pattern matching and algebraic datatypes
  • Persistent structures and immutability
  • Higher-order functions and lambda expressions
  • Lazy evaluation and streams
  • Threads and thread pools
  • Atomicity and locking
  • Synchronization and thread-safe objects
  • Lock-free, non-blocking patterns
  • Futures, promises, and functional-concurrent programming


As a bonus, the book includes a discussion of common typing strategies used in modern programming languages, including type inference, subtyping, polymorphism, type classes, type bounds, and type variance.

Most of the code examples are in Scala, which includes many of the standard features of functional and concurrent programming; however, no prior knowledge of Scala is assumed. You should be familiar with concepts such as classes, methods, objects, types, variables, loops, and conditionals and have enough programming experience to not be distracted by simple matters of syntax.

Foreword by Cay Horstmann xxiii Preface xxv Acknowledgments xxxv About the Author xxxvii
Part I. Functional Programming 1
Chapter 1: Concepts of Functional Programming 3
1.1 What Is Functional Programming? 3
1.2 Functions 4
1.3 From Functions to Functional Programming Concepts 6
1.4 Summary 7
Chapter 2: Functions in Programming Languages 9
2.1 Defining Functions 9
2.2 Composing Functions 10
2.3 Functions Defined as Methods 12
2.4 Operators Defined as Methods 12
2.5 Extension Methods 13
2.6 Local Functions 14
2.7 Repeated Arguments 15
2.8 Optional Arguments 16
2.9 Named Arguments 16
2.10 Type Parameters 17
2.11 Summary 19
Chapter 3: Immutability 21
3.1 Pure and Impure Functions 21
3.2 Actions 23
3.3 Expressions Versus Statements 25
3.4 Functional Variables 26
3.5 Immutable Objects 28
3.6 Implementation of Mutable State 29
3.7 Functional Lists 31
3.8 Hybrid Designs 32
3.9 Updating Collections of Mutable/Immutable Objects 35
3.10 Summary 36
Chapter 4: Case Study: Active-Passive Sets 39
4.1 Object-Oriented Design 39
4.2 Functional Values 41
4.3 Functional Objects 43
4.4 Summary 44
Chapter 5: Pattern Matching and Algebraic Data Types 47
5.1 Functional Switch 47
5.2 Tuples 48
5.3 Options 50
5.4 Revisiting Functional Lists 51
5.5 Trees 53
5.6 Illustration: List Zipper 56
5.7 Extractors 59
5.8 Summary 60
Chapter 6: Recursive Programming 63
6.1 The Need for Recursion 63
6.2 Recursive Algorithms 65
6.3 Key Principles of Recursive Algorithms 67
6.4 Recursive Structures 69
6.5 Tail Recursion 71
6.6 Examples of Tail Recursive Functions 73
6.7 Summary 77
Chapter 7: Recursion on Lists 79
7.1 Recursive Algorithms as Equalities 79
7.2 Traversing Lists 80
7.3 Returning Lists 82
7.4 Building Lists from the Execution Stack 84
7.5 Recursion on Multiple/Nested Lists 85
7.6 Recursion on Sublists Other Than the Tail 88
7.7 Building Lists in Reverse Order 90
7.8 Illustration: Sorting 92
7.9 Building Lists Efficiently 94
7.10 Summary 96
Chapter 8: Case Study: Binary Search Trees 99
8.1 Binary Search Trees 99
8.2 Sets of Integers as Binary Search Trees 100
8.3 Implementation Without Rebalancing 102
8.4 Self-Balancing Trees 107
8.5 Summary 113
Chapter 9: Higher-Order Functions 115
9.1 Functions as Values 115
9.2 Currying 118
9.3 Function Literals 120
9.4 Functions Versus Methods 123
9.5 Single-Abstract-Method Interfaces 124
9.6 Partial Application 125
9.7 Closures 130
9.8 Inversion of Control 133
9.9 Summary 133
Chapter 10: Standard Higher-Order Functions 137
10.1 Functions with Predicate Arguments 137
10.2 map and foreach 140
10.3 atMap 141
10.4 fold and reduce 146
10.5 iterate, tabulate, and unfold 148
10.6 sortWith, sortBy, maxBy, and minBy 149
10.7 groupBy and groupMap 150
10.8 Implementing Standard Higher-Order Functions 152
10.9 foreach, map, atMap, and for-Comprehensions 152
10.10 Summary 155
Chapter 11: Case Study: File Systems as Trees 157
11.1 Design Overview 157
11.2 A Node-Searching Helper Function 158
11.3 String Representation 158
11.4 Building Trees 160
11.5 Querying 164
11.6 Navigation 168
11.7 Tree Zipper 169
11.8 Summary 172
Chapter 12: Lazy Evaluation 173
12.1 Delayed Evaluation of Arguments 173
12.2 By-Name Arguments 174
12.3 Control Abstraction 176
12.4 Internal Domain-Specifc Languages 179
12.5 Streams as Lazily Evaluated Lists 180
12.6 Streams as Pipelines 182
12.7 Streams as Infinite Data Structures 184
12.8 Iterators 184
12.9 Lists, Streams, Iterators, and Views 187
12.10 Delayed Evaluation of Fields and Local Variables 190
12.11 Illustration: Subset-Sum 191
12.12 Summary 193
Chapter 13: Handling Failures 195
13.1 Exceptions and Special Values 195
13.2 Using Option 197
13.3 Using Try 198 &