Foreword....17
Preface....21
Acknowledgments....23
Introduction....25
Whom Is This Book For?....26
How Is This Book Organized?....26
Additional Resources....27
Chapter 1: Meet F#....29
F# in Visual Studio....30
Project Templates....30
Project Organization....32
Significance of Whitespace....33
Grouping Constructs....34
Namespaces....34
Modules....35
Expressions Are Everywhere....36
Application Entry Point....37
Implicit Return Values....37
Your First F# Program....38
Summary....40
Chapter 2: F# Interactive....41
Running F# Interactive....41
F# Interactive Output....43
The it Identifier....43
Playing in the Sandbox....43
#help....44
#quit....44
#load....44
#r....45
#I....45
#time....45
Scripting....46
F# Interactive Options....48
--load....48
--use....49
--reference....49
--lib....49
--define....49
--exec....50
--....50
--quiet....50
--optimize....51
--tailcalls....51
Summary....51
Chapter 3: Fundamentals....53
Immutability and Side Effects....54
Functional Purity....55
Bindings....56
let Bindings....56
use Bindings....58
do Bindings....61
Identifier Naming....61
Core Data Types....61
Boolean Values and Operators....62
Numeric Types....62
Characters....65
Strings....65
Type Inference....67
Nullability....69
Options....69
Unit Type....70
Enumerations....71
Flags Enumerations....71
Reconstructing Enumeration Values....73
Flow Control....73
Looping....73
Branching....75
Generics....76
Automatic Generalization....77
Explicit Generalization....78
Flexible Types....80
Wildcard Pattern....80
Statically Resolved Type Parameters....80
When Things Go Wrong....81
Handling Exceptions....81
Raising Exceptions....83
Custom Exceptions....84
String Formatting....86
Type Abbreviations....87
Comments....87
End-of-Line Comments....88
Block Comments....88
XML Documentation....88
Summary....90
Chapter 4: Staying Objective....91
Classes....92
Constructors....92
Fields....96
Properties....97
Methods....101
Events....105
Structures....108
Inheritance....109
Casting....110
Overriding Members....111
Abstract Classes....112
Abstract Members....113
Virtual Members....115
Sealed Classes....115
Static Members....116
Static Initializers....116
Static Fields....117
Static Properties....117
Static Methods....118
Mutually Recursive Types....119
Interfaces....119
Implementing Interfaces....120
Defining Interfaces....121
Custom Operators....122
Prefix Operators....122
Infix Operators....123
New Operators....124
Global Operators....124
Object Expressions....125
Type Extensions....127
Summary....128
Chapter 5: Let’s Get Functional....131
What Is Functional Programming?....132
Programming with Functions....132
Functions as Data....132
Interoperability Considerations....133
Currying....134
Partial Application....134
Pipelining....135
Function Composition....136
Recursive Functions....137
Tail-Call Recursion....138
Mutually Recursive Functions....139
Lambda Expressions....140
Closures....140
Functional Types....141
Tuples....141
Record Types....146
Discriminated Unions....150
Defining Discriminated Unions....151
Additional Members....157
Lazy Evaluation....158
Summary....160
Chapter 6: Going to Collections....161
Sequences....162
Creating Sequences....162
Working with Sequences....165
Arrays....170
Creating Arrays....170
Working with Arrays....172
Multidimensional Arrays....175
Jagged Arrays....176
Lists....177
Creating Lists....177
Working with Lists....178
Sets....180
Creating Sets....180
Working with Sets....181
Maps....183
Creating Maps....184
Working with Maps....184
Converting Between Collection Types....185
Summary....186
Chapter 7: Patterns, Patterns, Everywhere....187
Match Expressions....187
Guard Clauses....188
Pattern-Matching Functions....189
Exhaustive Matching....190
Variable Patterns....191
The Wildcard Pattern....191
Matching Constant Values....192
Identifier Patterns....192
Matching Union Cases....192
Matching Literals....193
Matching Nulls....193
Matching Tuples....194
Matching Records....195
Matching Collections....196
Array Patterns....196
List Patterns....196
Cons Patterns....197
Matching by Type....197
Type-Annotated Patterns....197
Dynamic Type-Test Patterns....198
As Patterns....199
Combining Patterns with And....199
Combining Patterns with OR....200
Parentheses in Patterns....200
Active Patterns....201
Partial Active Patterns....202
Parameterized Active Patterns....204
Summary....204
Chapter 8: Measuring Up....205
Defining Measures....206
Measure Formulas....206
Applying Measures....207
Stripping Measures....208
Enforcing Measures....209
Ranges....210
Converting Between Measures....210
Static Conversion Factors....210
Static Conversion Functions....211
Generic Measures....212
Custom Measure-Aware Types....212
Summary....213
Chapter 9: Can I Quote You on That?....215
Comparing Expression Trees and Quoted Expressions....216
Composing Quoted Expressions....218
Quoted Literals....218
.NET Reflection....219
Manual Composition....220
Splicing Quoted Expressions....222
Decomposing Quoted Expressions....222
Parsing Quoted Expressions....223
Substituting Reflection....225
Summary....227
Chapter 10: Show Me the Data....229
Query Expressions....229
Basic Querying....231
Filtering Data....232
Accessing Individual Items....235
Sorting Results....237
Grouping....238
Paginating....239
Aggregating Data....241
Detecting Items....242
Joining Multiple Data Sources....243
Extending Query Expressions....247
Type Providers....249
Available Type Providers....250
Using Type Providers....251
Example: Accessing an OData Service....252
Example: Parsing a String with RegexProvider....254
Summary....256
Chapter 11: Asynchronous and Parallel Programming....257
Task Parallel Library....258
Potential Parallelism....259
Data Parallelism....259
Task Parallelism....262
Asynchronous Workflows....269
Creating and Starting Asynchronous Workflows....270
Cancelling Asynchronous Workflows....273
Exception Handling....275
Asynchronous Workflows and the Task Parallel Library....276
Agent-Based Programming....278
Getting Started....278
Scanning for Messages....279
Replying to Messages....280
Example: Agent-Based Calculator....281
Summary....283
Chapter 12: Computation Expressions....285
Anatomy of a Computation Expression....286
Example: FizzBuzz....289
Example: Building Strings....292
Summary....297
Index....299
F# brings the power of functional-first programming to the .NET Framework, a platform for developing software in the Microsoft Windows ecosystem. If you're a traditional .NET developer used to C# and Visual Basic, discovering F# will be a revelation that will change how you code, and how you think about coding.
In The Book of F#, Microsoft MVP Dave Fancher shares his expertise and teaches you how to wield the power of F# to write succinct, reliable, and predictable code. As you learn to take advantage of features like default immutability, pipelining, type inference, and pattern matching, you'll be amazed at how efficient and elegant your code can be.
Break free of that old school of programming. The Book of F# will show you how to unleash the expressiveness of F# to create smarter, leaner code.