The Book of F#

Book description

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.

You'll also learn how to:

  • Exploit F#'s functional nature using currying, partial application, and delegation
  • Streamline type creation and safety with record types and discriminated unions
  • Use collection types and modules to handle data sets more effectively
  • Use pattern matching to decompose complex types and branch your code within a single expression
  • Make your software more responsive with parallel programming and asynchronous workflows
  • Harness object orientation to develop rich frameworks and interact with code written in other .NET languages
  • Use query expressions and type providers to access and manipulate data sets from disparate sources

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.

Publisher resources

View/Submit Errata

Table of contents

  1. Copyright
  2. About the Author
    1. About the Author
    2. About the Technical Reviewer
  3. Foreword
  4. Preface
  5. Acknowledgments
  6. Introduction
    1. Whom Is This Book For?
    2. How Is This Book Organized?
    3. Additional Resources
  7. 1. Meet F#
    1. F# in Visual Studio
      1. Project Templates
      2. Project Organization
    2. Significance of Whitespace
    3. Grouping Constructs
      1. Namespaces
      2. Modules
    4. Expressions Are Everywhere
    5. Application Entry Point
    6. Implicit Return Values
    7. Your First F# Program
    8. Summary
  8. 2. F# Interactive
    1. Running F# Interactive
    2. F# Interactive Output
    3. The it Identifier
    4. Playing in the Sandbox
      1. #help
      2. #quit
      3. #load
      4. #r
      5. #I
      6. #time
    5. Scripting
    6. F# Interactive Options
      1. --load
      2. --use
      3. --reference
      4. --lib
      5. --define
      6. --exec
      7. --
      8. --quiet
      9. --optimize
      10. --tailcalls
    7. Summary
  9. 3. Fundamentals
    1. Immutability and Side Effects
    2. Functional Purity
    3. Bindings
      1. let Bindings
        1. Literals
        2. Mutable Bindings
        3. Reference Cells
      2. use Bindings
        1. using Function
        2. Replicating the using Function in C#
      3. do Bindings
    4. Identifier Naming
    5. Core Data Types
      1. Boolean Values and Operators
      2. Numeric Types
        1. Numeric Operators
        2. Numeric Conversion Functions
      3. Characters
      4. Strings
        1. String Literals
        2. Verbatim Strings
        3. Triple-Quoted Strings
        4. String Concatenation
    6. Type Inference
    7. Nullability
      1. Options
      2. Unit Type
    8. Enumerations
      1. Flags Enumerations
      2. Reconstructing Enumeration Values
    9. Flow Control
      1. Looping
        1. while Loops
        2. for Loops
      2. Branching
    10. Generics
      1. Automatic Generalization
      2. Explicit Generalization
      3. Flexible Types
      4. Wildcard Pattern
      5. Statically Resolved Type Parameters
    11. When Things Go Wrong
      1. Handling Exceptions
        1. try. . .with Expressions
        2. try. . .finally Expressions
      2. Raising Exceptions
      3. Custom Exceptions
    12. String Formatting
    13. Type Abbreviations
    14. Comments
      1. End-of-Line Comments
      2. Block Comments
      3. XML Documentation
    15. Summary
  10. 4. Staying Objective
    1. Classes
      1. Constructors
        1. Primary Constructors
        2. Additional Constructors
        3. Self-Identifiers
      2. Fields
        1. let Bindings
        2. Explicit Fields
      3. Properties
        1. Explicit Properties
        2. Implicit Properties
        3. Indexed Properties
        4. Setting at Initialization
      4. Methods
        1. Instance Methods
        2. Method Accessibility
        3. Named Arguments
        4. Overloaded Methods
        5. Optional Parameters
        6. Slice Expressions
      5. Events
        1. Basic Event Handling
        2. Observing Events
        3. Custom Events
    2. Structures
    3. Inheritance
      1. Casting
        1. Upcasting
        2. Downcasting
      2. Overriding Members
      3. Abstract Classes
      4. Abstract Members
        1. Abstract Properties
        2. Abstract Methods
      5. Virtual Members
      6. Sealed Classes
    4. Static Members
      1. Static Initializers
      2. Static Fields
      3. Static Properties
      4. Static Methods
    5. Mutually Recursive Types
    6. Interfaces
      1. Implementing Interfaces
      2. Defining Interfaces
    7. Custom Operators
      1. Prefix Operators
      2. Infix Operators
      3. New Operators
      4. Global Operators
    8. Object Expressions
    9. Type Extensions
    10. Summary
  11. 5. Let’s Get Functional
    1. What Is Functional Programming?
    2. Programming with Functions
      1. Functions as Data
      2. Interoperability Considerations
    3. Currying
      1. Partial Application
      2. Pipelining
        1. Forward Pipelining
        2. Backward Pipelining
        3. Noncurried Functions
      3. Function Composition
    4. Recursive Functions
      1. Tail-Call Recursion
      2. Mutually Recursive Functions
    5. Lambda Expressions
    6. Closures
    7. Functional Types
      1. Tuples
        1. Extracting Values
        2. Equality Semantics
        3. Syntactic Tuples
        4. Out Parameters
      2. Record Types
        1. Defining Record Types
        2. Creating Records
        3. Avoiding Naming Conflicts
        4. Copying Records
        5. Mutability
        6. Additional Members
    8. Discriminated Unions
      1. Defining Discriminated Unions
        1. Simple Object Hierarchies
        2. Tree Structures
        3. Replacing Type Abbreviations
      2. Additional Members
    9. Lazy Evaluation
    10. Summary
  12. 6. Going to Collections
    1. Sequences
      1. Creating Sequences
        1. Sequence Expressions
        2. Range Expressions
        3. Empty Sequences
        4. Initializing a Sequence
      2. Working with Sequences
        1. Finding Sequence Length
        2. Iterating over Sequences
        3. Transforming Sequences
        4. Sorting Sequences
        5. Filtering Sequences
        6. Aggregating Sequences
    2. Arrays
      1. Creating Arrays
        1. Array Expressions
        2. Empty Arrays
        3. Initializing Arrays
      2. Working with Arrays
        1. Accessing Elements
        2. Copying Arrays
        3. Sorting Arrays
      3. Multidimensional Arrays
      4. Jagged Arrays
    3. Lists
      1. Creating Lists
      2. Working with Lists
        1. Accessing Elements
        2. Combining Lists
    4. Sets
      1. Creating Sets
      2. Working with Sets
        1. Unions
        2. Intersections
        3. Differences
        4. Subsets and Supersets
    5. Maps
      1. Creating Maps
      2. Working with Maps
        1. Finding Values
        2. Finding Keys
    6. Converting Between Collection Types
    7. Summary
  13. 7. Patterns, Patterns, Everywhere
    1. Match Expressions
      1. Guard Clauses
      2. Pattern-Matching Functions
    2. Exhaustive Matching
      1. Variable Patterns
      2. The Wildcard Pattern
    3. Matching Constant Values
    4. Identifier Patterns
      1. Matching Union Cases
      2. Matching Literals
    5. Matching Nulls
    6. Matching Tuples
    7. Matching Records
    8. Matching Collections
      1. Array Patterns
      2. List Patterns
      3. Cons Patterns
    9. Matching by Type
      1. Type-Annotated Patterns
      2. Dynamic Type-Test Patterns
    10. As Patterns
    11. Combining Patterns with AND
    12. Combining Patterns with OR
    13. Parentheses in Patterns
    14. Active Patterns
    15. Partial Active Patterns
    16. Parameterized Active Patterns
    17. Summary
  14. 8. Measuring Up
    1. Defining Measures
    2. Measure Formulas
    3. Applying Measures
    4. Stripping Measures
    5. Enforcing Measures
    6. Ranges
    7. Converting Between Measures
      1. Static Conversion Factors
      2. Static Conversion Functions
    8. Generic Measures
    9. Custom Measure-Aware Types
    10. Summary
  15. 9. Can I Quote You on that?
    1. Comparing Expression Trees and Quoted Expressions
    2. Composing Quoted Expressions
      1. Quoted Literals
      2. .NET Reflection
      3. Manual Composition
      4. Splicing Quoted Expressions
    3. Decomposing Quoted Expressions
      1. Parsing Quoted Expressions
      2. Substituting Reflection
    4. Summary
  16. 10. Show Me the Data
    1. Query Expressions
      1. Basic Querying
      2. Filtering Data
        1. Predicate-Based Filters
        2. Distinct-Item Filters
      3. Accessing Individual Items
        1. Getting the First or Last Item
        2. Getting an Arbitrary Item
      4. Sorting Results
        1. Sorting in Ascending Order
        2. Sorting in Descending Order
        3. Sorting by Multiple Values
      5. Grouping
      6. Paginating
      7. Aggregating Data
      8. Detecting Items
      9. Joining Multiple Data Sources
      10. Extending Query Expressions
        1. Example: ExactlyOneWhen
        2. Example: AverageByNotNull
    2. Type Providers
      1. Available Type Providers
      2. Using Type Providers
      3. Example: Accessing an OData Service
      4. Example: Parsing a String with RegexProvider
    3. Summary
  17. 11. Asynchronous and Parallel Programming
    1. Task Parallel Library
      1. Potential Parallelism
      2. Data Parallelism
        1. Locking and Lock Avoidance
        2. Short-Circuiting Parallel Loops
        3. Cancelling Parallel Loops
      3. Task Parallelism
        1. Creating and Starting Tasks
        2. Returning Values from Tasks
        3. Waiting for Task Completion
        4. Continuations
        5. Cancelling Tasks
        6. Exception Handling
    2. Asynchronous workflows
      1. Creating and Starting Asynchronous Workflows
      2. Cancelling Asynchronous Workflows
      3. Exception Handling
      4. Asynchronous Workflows and the Task Parallel Library
    3. Agent-Based Programming
      1. Getting Started
      2. Scanning for Messages
      3. Replying to Messages
      4. Example: Agent-Based Calculator
    4. Summary
  18. 12. Computation Expressions
    1. Anatomy of a Computation Expression
    2. Example: FizzBuzz
    3. Example: Building Strings
    4. Summary
  19. Index
  20. About the Author

Product information

  • Title: The Book of F#
  • Author(s): Dave Fancher
  • Release date: March 2014
  • Publisher(s): No Starch Press
  • ISBN: 9781593275525