مشخصات کتاب
-
Chris Smith
-
2010
-
انگلیسی
-
4810
-
735
-
1
Programming F#
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Part I. Multiparadigm Programming
1. Introduction to F# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Getting to Know F# 3
Visual Studio 2010 4
Your Second F# Program 5
Values 6
Whitespace Matters 6
.NET Interop 8
Comments 8
F# Interactive 8
Managing F# Source Files 11
2. Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Primitive Types 13
Numeric Primitives 14
Arithmetic 15
Conversion Routines 17
BigInt 17
Bitwise Operations 18
Characters 18
Strings 19
Boolean Values 20
Comparison and Equality 21
Functions 22
Type Inference 23
Generic Functions 24
Scope 25
Control Flow 27
Core Types 29
Unit 29
Tuple 30
Lists 32
Aggregate Operators 36
Option 39
Printfn 41
Anatomy of an F# Program 42
Modules 43
Namespaces 43
Program Startup 44
3. Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Programming with Functions 48
Immutability 49
Function Values 50
Recursive Functions 53
Symbolic Operators 55
Function Composition 57
Pattern Matching 62
Match Failure 64
Named Patterns 64
Matching Literals 65
when Guards 66
Grouping Patterns 67
Matching the Structure of Data 68
Outside of Match Expressions 69
Alternate Lambda Syntax 70
Discriminated Unions 70
Using Discriminated Unions for Tree Structures 72
Pattern Matching 73
Methods and Properties 75
Records 75
Cloning Records 76
Pattern Matching 77
Type Inference 77
Methods and Properties 78
Lazy Evaluation 79
Lazy Types 79
Sequences 80
Sequence Expressions 81
Seq Module Functions 82
Aggregate Operators 83
4. Imperative Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Understanding Memory in .NET 86
Value Types Versus Reference Types 87
Default Values 87
Reference Type Aliasing 89
Changing Values 89
Reference Cells 91
Mutable Records 92
Arrays 92
Indexing an Array 93
Array Slices 95
Creating Arrays 96
Pattern Matching 97
Array Equality 97
Array Module Functions 98
Multidimensional Arrays 99
Mutable Collection Types 101
List<'T> 101
Dictionary<'K,'V> 102
HashSet<'T> 104
Looping Constructs 105
While Loops 105
For Loops 106
Exceptions 108
Handling Exceptions 109
Reraising Exceptions 111
Defining Exceptions 111
5. Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Programming with Objects 115
The Benefits of OOP 115
When OOP Breaks Down 116
Understanding System.Object 116
Common Methods 117
Object Equality 119
Generated Equality 120
Understanding Classes 122
Explicit Construction 123
Implicit Class Construction 124
Generic Classes 125
The Self-Identifier 127
Methods and Properties 127
Properties 128
Setting Properties in the Constructor 129
Methods 129
Static Methods, Properties, and Fields 131
Method Overloading 133
Accessibility Modifiers 134
Inheritance 136
Method Overriding 138
Categories of Classes 139
Casting 141
6. .NET Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
The .NET Platform 145
The CLI 145
Garbage Collection 146
Interfaces 148
Using Interfaces 149
Defining Interfaces 150
Object Expressions 151
Object Expressions for Interfaces 152
Object Expressions for Derived Classes 153
Extension Methods 154
Extending Modules 155
Enumerations 156
Creating Enumerations 156
Conversion 157
When to Use an Enum Versus a Discriminated Union 158
Structs 159
Creating Structs 159
Restrictions 161
When to Use a Struct Versus a Record 161
7. Applied Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Units of Measure 163
Defining Units of Measure 165
Converting Between Units of Measure 166
Generic Units of Measure 167
Active Patterns 168
Single-Case Active Patterns 169
Partial-Case Active Patterns 170
Parameterized Active Patterns 172
Multi-Case Active Patterns 173
Using Active Patterns 174
Using Modules 178
Converting Modules to Classes 178
Intentional Shadowing 181
Controlling Module Usage 182
Mastering Lists 183
List Operations 184
Using Lists 185
Tail Recursion 187
Understanding the Stack 188
Introducing Tail Recursion 190
Tail-Recursive Patterns 192
Programming with Functions 196
Currying 196
Eliminating Redundant Code 197
Closures 198
Functional Patterns 199
Memoization 199
Mutable Function Values 202
Lazy Programming 203
8. Applied Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Operators 205
Operator Overloading 205
Indexers 207
Adding Slices 209
Generic Type Constraints 211
Delegates and Events 214
Defining Delegates 215
Combining Delegates 217
Events 218
Creating Events 218
The Event<_,_> Class 220
The Observable Module 221
Creating .NET Events 225
Part II. Programming F#
9. Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
F# Script Files 230
Directives 230
General Directives 231
F# Script-Specific Directives 231
F# Script Recipes 234
Colorful Output 234
Producing Sound 235
Walking a Directory Structure 235
Starting Processes Easily 236
Automating Microsoft Office 237
10. Computation Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Toward Computation Expressions 241
Computation Expression Builders 244
Custom Computation Expression Builders 247
Asynchronous Workflows 248
The Rounding Workflow 249
The State Workflow 250
11. Asynchronous and Parallel Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Working with Threads 258
Spawning Threads 259
The .NET Thread Pool 260
Sharing Data 261
Asynchronous Programming 265
Asynchronous Workflows 268
The Async Library 269
Async Operations 273
Custom Async Primitives 274
Limitations 275
Parallel Programming 276
Parallel.For 276
The Array.Parallel Module 277
Parallel Extensions for .NET 278
Primitives 279
Concurrent Data Structures 283
12. Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Attributes 287
Applying Attributes 289
Defining New Attributes 290
Type Reflection 291
Accessing Types 291
Reflecting on F# Types 296
Dynamic Instantiation 298
Instantiating Types 298
Instantiating F# Types 299
Dynamic Invocation 299
The Question Mark Operators 300
Using Reflection 301
Declarative Programming 302
Plug-in Architecture 305
13. Quotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Quotation Basics 312
Decomposing Quotations 312
Quoting Method Bodies 316
Decomposing Arbitrary Code 318
Application: Deferring Computation to Other Platforms 319
Generating Quotation Expressions 321
Expression Holes 322
Evaluating Quotations 322
Application: Generating Derivatives 324
A. Overview of .NET Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
B. F# Interop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371