Contents

images About the Authors

images About the Technical Reviewer

images Acknowledgments

images Chapter 1: Introduction

The Genesis of F#

About This Book

Who This Book Is For

images Chapter 2: Your First F# Program – Getting Started With F#

Creating Your First F# Program

Documenting Code

Using let

Understanding Types

Calling Functions

Lightweight Syntax

Understanding Scope

Using Data Structures

Using Properties and the Dot-Notation

Using Tuples

Using Imperative Code

Using Object-Oriented Libraries from F#

Using open to Access Namespaces and Modules

Using new and Setting Properties

Fetching a Web Page

Summary

images Chapter 3: Introducing Functional Programming

Starting with Numbers and Strings

Some Simple Types and Literals

Arithmetic Conversions

Arithmetic Comparisons

Simple Strings

Working with Conditionals: && and ||

Defining Recursive Functions

Lists

Options

Getting Started with Pattern Matching

Matching on Structured Values

Guarding Rules and Combining Patterns

Further Ways of Forming Patterns

Introducing Function Values

Using Anonymous Function Values

Computing with Aggregate Operators

Composing Functions with >>

Building Functions with Partial Application

Using Local Functions

Iterating with Aggregate Operators

Abstracting Control with Functions

Using Object Methods as First-Class Functions

Some Common Uses of Function Values

Summary

images Chapter 4: Introducing Imperative Programming

About Functional and Imperative Programming

Imperative Looping and Iterating

Simple for Loops

Simple While Loops

More Iteration Loops over Sequences

Using Mutable Records

Mutable Reference Cells

Avoiding Aliasing

Hiding Mutable Data

Using Mutable Locals

Working with Arrays

Generating and Slicing Arrays

Two-Dimensional Arrays

Introducing the Imperative .NET Collections

Using Resizeable Arrays

Using Dictionaries

Using Dictionary’s TryGetValue

Using Dictionaries with Compound Keys

Some Other Mutable Data Structures

Exceptions and Controlling Them

Catching Exceptions

Using try . . . finally

Defining New Exception Types

Having an Effect: Basic I/O

.NET I/O via Streams

Some Other I/O-Related Types

Using System.Console

Combining Functional and Imperative: Efficient Precomputation and Caching

Precomputation and Partial Application

Precomputation and Objects

Memoizing Computations

Lazy Values

Other Variations on Caching and Memoization

Combining Functional and Imperative: Functional Programming with Side Effects

Consider Replacing Mutable Locals and Loops with Recursion

Separating Pure Computation from Side-Effecting Computations

Separating Mutable Data Structures

Not All Side Effects Are Equal

Avoid Combining Imperative Programming and Laziness

Summary

images Chapter 5: Understanding Types in Functional Programming

Exploring Some Simple Type Definitions

Defining Type Abbreviations

Defining Record Types

Handling Non-Unique Record Field Names

Cloning Records

Defining Discriminated Unions

Using Discriminated Unions as Records

Defining Multiple Types Simultaneously

Understanding Generics

Writing Generic Functions

Some Important Generic Functions

Making Things Generic

Generic Algorithms through Explicit Arguments

Generic Algorithms through Function Parameters

Generic Algorithms through Inlining

More on Different Kinds of Types

Reference Types and Value Types

Other Flavors of .NET Types

Understanding Subtyping

Casting Up Statically

Casting Down Dynamically

Performing Type Tests via Pattern Matching

Knowing When Upcasts Are Applied Automatically

Flexible Types

Troubleshooting Type-Inference Problems

Using a Visual Editing Environment

Using Type Annotations

Understanding the Value Restriction

Working Around the Value Restriction

Understanding Generic Overloaded Operators

Summary

images Chapter 6: Programming with Objects

Getting Started with Objects and Members

Using Classes

Adding Further Object Notation to Your Types

Working with Indexer Properties

Adding Overloaded Operators

Using Named and Optional Arguments

Adding Method Overloading

Defining Object Types with Mutable State

Using Optional Property Settings

Declaring Auto-Properties

Getting Started with Object Interface Types

Defining New Object Interface Types

Implementing Object Interface Types Using Object Expressions

Implementing Object Interface Types Using Concrete Types

Using Common Object Interface Types from the .NET Libraries

Understanding Hierarchies of Object Interface Types

More Techniques to Implement Objects

Combining Object Expressions and Function Parameters

Defining Partially Implemented Class Types

Using Partially Implemented Types via Delegation

Using Partially Implemented Types via Implementation Inheritance

Combining Functional and Objects: Cleaning Up Resources

Resources and IDisposable

Managing Resources with More Complex Lifetimes

Cleaning Up Internal Objects

Cleaning Up Unmanaged Objects

Extending Existing Types and Modules

Working with F# Objects and .NET Types

Structs

Delegates

Enums

Working with null Values

Summary

images Chapter 7: Encapsulating and Organizing Your Code

Hiding Things

Hiding Things with Local Definitions

Hiding Things with Accessibility Annotations

Organizing Code with Namespaces and Modules

Putting Your Code in a Module

Putting Your Modules and Types in Namespaces

Hiding Things with Signatures

Designing with Signatures

When Are Signature Types Checked?

Defining a Module with the Same Name as a Type

Preventing Client Code from Opening a Module

Using Files as Modules

Automatically Opening Modules

Reusing Your Code

Using Files as Small Reusable Components

Creating Assemblies, DLLs, and EXEs

Creating and Sharing Packages

Summary

images Chapter 8: Working with Textual Data

Building Strings and Formatting Data

Building Strings

More about String Literals

Using printf and Friends

Generic Structural Formatting

Formatting Strings Using .NET Formatting

Parsing Strings and Textual Data

Parsing Basic Values

Processing Line-Based Input

Using Regular Expressions to Parse Lines

More on Matching with System.Text.RegularExpressions

Encoding and Decoding Unicode Strings

Encoding and Decoding Binary Data

Using XML as a Concrete Language Format

Using the System.Xml Namespace

From Concrete XML to Abstract Syntax

Some Recursive Descent Parsing

A Simple Tokenizer

Recursive-Descent Parsing

Binary Parsing and Formatting

Summary

images Chapter 9: Working with Sequences and Structured Data

Getting Started with Sequences

Using Range Expressions

Iterating a Sequence

Transforming Sequences with Aggregate Operators

Which Types Can Be Used as Sequences?

Using Lazy Sequences from External Sources

Using Sequence Expressions

Enriching Sequence Expressions with Additional Logic

Generating Lists and Arrays Using Sequence Expressions

More on Working with Sequences

Using Other Sequence Operators: Truncate and Sort

Selecting Multiple Elements From Sequences

Finding Elements and Indexes in Sequences

Grouping and Indexing Sequences

Folding Sequences

Cleaning Up in Sequence Expressions

Expressing Some Operations Using Sequence Expressions

Structure Beyond Sequences: Working with Trees

Example: Abstract Syntax Representations

Transforming Abstract Syntax Representations

Using On-Demand Computation with Abstract Syntax Trees

Caching Properties in Abstract Syntax Trees

Memoizing Construction of Syntax Tree Nodes

Active Patterns: Views for Structured Data

Converting the Same Data to Many Views

Matching on .NET Object Types

Defining Partial and Parameterized Active Patterns

Hiding Abstract Syntax Implementations with Active Patterns

Equality, Hashing, and Comparison for New Structured Data Types

Equality, Hashing, and Comparison

Asserting Equality, Hashing, and Comparison Using Attributes

Fully Customizing Equality, Hashing, and Comparison on a Type

Suppressing Equality, Hashing, and Comparison on a Type

Customizing Generic Collection Types

Tail Calls and Recursive Programming

Tail Recursion and List Processing

Tail Recursion and Object-Oriented Programming

Tail Recursion and Processing Unbalanced Trees

Using Continuations to Avoid Stack Overflows

Another Example: Processing Syntax Trees

Summary

images Chapter 10: Numeric Programming and Charting

Basic Charting with FSharpChart

Basic Numeric Types and Literals

Arithmetic Operators

Checked Arithmetic

Arithmetic Conversions

Arithmetic Comparisons

Overloaded Math Functions

Bitwise Operations

Sequences, Statistics and Numeric Code

Summing, Averaging, Maximizing and Minimizing Sequences

Counting and Categorizing

Writing Fresh Numeric Code

Making Numeric Code Generic

Example: KMeans

Statistics, Linear Algebra and Distributions with Math.NET

Basic Statistical Functions in Math.NET Numerics

Using Histograms and Distributions from Math.NET Numerics

Using Matrices and Vectors from Math.NET

Matrix Inverses, Decompositions and Eigenvalues

Units of Measure

Adding Units to a Numeric Algorithms

Adding Units to a Type Definition

Applying and Removing Units

Some Limitations of Units of Measure

Summary

images Chapter 11: Reactive, Asynchronous, and Parallel Programming

Introducing Some Terminology

Events

Events as First-Class Values

Creating and Publishing Events

Using and Designing Background Workers

Building a Simpler Iterative Worker

Raising Additional Events from Background Workers

Connecting a Background Worker to a GUI

Introducing Asynchronous and Parallel Computations

Fetching Multiple Web Pages in Parallel, Asynchronously

Understanding Thread Hopping

Under the Hood: What Are Asynchronous Computations?

Parallel File Processing Using Asynchronous Workflows

Running Asynchronous Computations

Common I/O Operations in Asynchronous Workflows

Using Tasks with Asynchronous Programming

Understanding Exceptions and Cancellation

Under the Hood: Implementing Async.Parallel

Using async for CPU Parallelism with Fixed Tasks

Agents

Introducing Agents

Creating Objects That React to Messages

Scanning Mailboxes for Relevant Messages

Example: Asynchronous Web Crawling

Observables

Using Shared-Memory Concurrency

Creating Threads Explicitly

Shared Memory, Race Conditions, and the .NET Memory Model

Using Locks to Avoid Race Conditions

Using ReaderWriterLock

Some Other Concurrency Primitives

Summary

images Chapter 12: Symbolic Programming with Structured Data

Verifying Circuits with Propositional Logic

Representing Propositional Logic

Evaluating Propositional Logic Naively

From Circuits to Propositional Logic

Checking Simple Properties of Circuits

Representing Propositional Formulae Efficiently Using BDDs

Circuit Verification with BDDs

Symbolic Differentiation and Expression Rendering

Modeling Simple Algebraic Expressions

Implementing Local Simplifications

A Richer Language of Algebraic Expressions

Parsing Algebraic Expressions

Simplifying Algebraic Expressions

Symbolic Differentiation of Algebraic Expressions

Rendering Expressions

Building the User Interface

Summary

images Chapter 13: Integrating External Data and Services

Some Basic REST Requests

Getting Data in JSON Format

Parsing the XML or JSON Data

Handling Multiple Pages

Getting Started with Type Providers and Queries

Example - Language Integrated OData

What is a Type Provider?

What is a Query?

Handling Pagination in OData

Example - Language Integrated SQL

More on Queries

Sorting

Aggregation

Nullables

Inner Queries

Grouping

Joins

More on Relational Databases and ADO.NET

Establishing Connections using ADO.NET

Creating a Database using ADO.NET

Creating Tables using ADO.NET

Using Stored Procedures via ADO.NET

Using WSDL Services

Summary

images Chapter 14: Building Smart Web Applications

Serving Web Content the Simple Way

Building Ajax Rich Client Applications

Learning More from the WebSharper Documentation

Getting Started with WebSharper

WebSharper Pagelets

Calling Server Code from the Client

WebSharper Sitelets

Online vs. Offline Sitelets

Serving Content from WebSharper Sitelets

Using Dynamic Templates

Embedding Client-Side Controls in Sitelet Pages

Constructing and Combining Sitelets

Sitelet Routers and Controllers

Constructing Sitelets for Handling Non-GET HTTP Commands

WebSharper Formlets

Dependent Formlets and Flowlets

Automated Resource Tracking and Handling

Using Third-Party JavaScript Libraries

Working with .NET Proxies

Summary

images Chapter 15: Building Mobile Web Applications

Web-based vs. Native Mobile Applications

Feature Detection and Polyfilling in WebSharper

Mobile Capabilities, Touch Events, and Mobile Frameworks

Serving Mobile Content

Building a Mobile Web Application for iOS Devices

Fleshing Out the Application

Digging Deeper

Developing Social Networking Applications

Configuring Your New Facebook Application

Defining the Main HTML Application

WebSharper Mobile

Developing Android Applications with WebSharper

Setting Up and Testing with Your Android Environment

Using the Android Application Visual Studio Template

Implementing Your Native Android Application

Summary

images Chapter 16: Visualization and Graphical User Interfaces

Writing “Hello, World!” in a Click

Understanding the Anatomy of a Graphical Application

Composing User Interfaces

Drawing Applications

Writing Your Own Controls

Developing a Custom Control

Anatomy of a Control

Displaying Samples from Sensors

Building the GraphControl: The Model

Building the GraphControl: Style Properties and Controller

Building the GraphControl: The View

Putting It Together

Creating a Mandelbrot Viewer

Computing Mandelbrot

Setting Colors

Creating the Visualization Application

Creating the Application Plumbing

Windows Presentation Foundation

When GUIs Meet the Web

Drawing

Controls

Bitmaps and Images

Final Considerations

Summary

images Chapter 17: Language-Oriented Programming: Advanced Techniques

Computation Expressions

An Example: Success/Failure Workflows

Defining a Workflow Builder

Workflows and Untamed Side Effects

Computation Expressions with Custom Query Operators

Example: Probabilistic Workflows

Combining Workflows and Resources

Recursive Workflow Expressions

Using F# Reflection

Reflecting on Types

Schema Compilation by Reflecting on Types

Using the F# Dynamic Reflection Operators

Using F# Quotations

Example: Using F# Quotations for Error Estimation

Resolving Reflected Definitions

Summary

images Chapter 18: Libraries and Interoperating with Other Languages

Types, memory and interoperability

Libraries: A High-Level Overview

Namespaces from the .NET Framework

Namespaces from the F# Libraries

Using the System Types

Using Further F# and .NET Data Structures

System.Collections.Generic and Other .NET Collections

Supervising and Isolating Execution

Further Libraries for Reflective Techniques

Using General Types

Using Microsoft.FSharp.Reflection

Some Other .NET Types You May Encounter

Under the Hood: Interoperating with C# and other .NET Languages

The Common Language Runtime

Memory Management at Runtime

COM Interoperability

Calling COM Components from F#

The Running Object Table

Interoperating with C and C++ with PInvoke

Getting Started with PInvoke

Mapping C Data Structures to F# Code

Marshalling Parameters to and from C

Marshalling Strings to and from C

Passing Function Pointers to C

PInvoke Memory Mapping

Wrapper Generation and Limits of PInvoke

Summary

images Chapter 19: Packaging, Debugging and Testing F# Code

Packaging Your Code

Mixing Scripting and Compiled Code

Choosing Optimization Settings

Generating Documentation

Building Shared Libraries

Using Static Linking

Packaging Different Kinds of Code

Using Data and Configuration Settings

Debugging Your Code

Using More Features of the Visual Studio Debugger

Instrumenting Your Program with the System.Diagnostics Namespace

Debugging Concurrent and Graphical Applications

Debugging and Testing with F# Interactive

Controlling F# Interactive

Some Common F# Interactive Directives

Understanding How F# Interactive Compiles Code

F# Interactive and Visual Studio

Testing Your Code

Summary

images Chapter 20: Designing F# Libraries

Designing Vanilla .NET Libraries

Understanding Functional Design Methodology

Understanding Where Functional Programming Comes From

Understanding Functional Design Methodology

Applying the .NET Library Design Guidelines to F#

Recommendation: Use the .NET Naming and Capitalization Conventions Where Possible

Recommendation: Avoid Using Underscores in Names

Recommendation: Follow the .NET Guidelines for Exceptions

Recommendation: Consider Using Option Values for Return Types Instead of Raising Exceptions

Recommendation: Follow the .NET Guidelines for Value Types

Recommendation: Consider Using Explicit Signature Files for Your Framework

Recommendation: Consider Avoiding the Use of Implementation Inheritance for Extensibility

Recommendation: Use Properties and Methods for Attributes and Operations Essential to a Type

Recommendation: Avoid Revealing Concrete Data Representations Such as Records

Recommendation: Use Active Patterns to Hide the Implementations of Discriminated Unions

Recommendation: Use Object Interface Types Instead of Tuples or Records of Functions

Recommendation: Understand When Currying Is Useful in Functional Programming APIs

Recommendation: Use Tuples for Return Values, Arguments, and Intermediate Values

Some Recommended Coding Idioms

Recommendation: Use the Standard Operators

Recommendation: Place the Pipeline Operator |> at the Start of a Line

Recommendation: Format Object Expressions Using the member Syntax

Summary

Index

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.123.155