F# Deep Dives
Edited by Tomas Petricek and Phillip Trelford
  • December 2014
  • ISBN 9781617291326
  • 372 pages
  • printed in black & white

Finally! A book that shows a wide variety of applications for F#.

Jonathan DeCarlo, Bentley Systems, Inc.

F# Deep Dives presents a collection of real-world F# techniques, each written by expert practitioners. Each chapter presents a new use case where you'll read how the author used F# to solve a complex problem more effectively than would have been possible using a traditional approach. You'll not only see how a specific solution works in a specific domain, you'll also learn how F# developers approach problems, what concepts they use to solve them, and how they integrate F# into existing systems and environments.

Table of Contents show full



about this book

1. Succeeding with functional-first languages in the industry

F# as part of an ecosystem

Building a healthy environment

F# from a business perspective

Understanding business problems and implications

Inferring business needs

Writing correct software

Reducing time to market

Managing complexity

Writing efficient and scalable software

Learning from case studies

Balancing the power-generation schedule

Analyzing data at Kaggle

the server side of online games


About the authors

Part 1 Introduction

2. Calculating cumulative binomial distributions

Implementing the formula

The formula for cumulative binomial distribution

Coding the formula

Adding tests

Adding NUnit and FsUnit

Generating test cases in Excel

Exposing the Fact function to unit testing

Returning large integers from the Fact function

Processing large integers in the Binomial function

Time for a rethink

An alternative approach

Implementing the Excel algorithm


Eliminating the cumulative flag

Identifying common functions between the two while loops

Eliminating duplication, mutables, and loops


About the author

3. Parsing text-based languages

Introducing the Markdown format

Formatting text with Markdown

Why another Markdown parser?

Representing Markdown documents

Parsing spans using recursive functions

Implementing the parser using active patterns

Parsing spans using active patterns

Parsing blocks using active patterns

Turning Markdown into HTML

Processing Markdown documents

Implementing the tree-processing patterns


About the author

Part 2 Developing analytical components

4. Numerical computing in the financial domain

Introducing financial derivatives and underlying assets

Non-dividend-paying stocks

European call options

Using probability functions of Math.NET

Configuring F# Interactive

Downloading and setting up Math.NET Numerics

Random variables, expectation, and variance

Generating normal random samples

Geometric Brownian motion and Monte Carlo estimates

Modeling stock prices using geometric Brownian motion

Payoff function, discounted payoff, and Monte Carlo estimates

Analyzing Monte Carlo estimates using variance

Pricing path-dependent options

Variance reduction using antithetic variates


About the author

5. Understanding social networks

Social networks on Twitter

Connecting to Twitter

Downloading the social network around the F# Software Foundation

Nodes in the Twitter network

Network representation in the JSON format

Visualization with D3.js

Visualization Parameters

Exploring the social network

Representing a network with an adjacency matrix

Reading JSON files with type providers

In-degrees and out-degrees

Finding the most-connected users

Using the R provider to visualize the degree distribution

Log degree distribution and scale-free networks


Mathematical formulation of PageRank

Calculating PageRank with a damping factor

Using MapReduce to compute PageRank

PageRank results

Visualizing important nodes


About the author

6. Integrating stock data into the F# language

Introducing type providers

Using the CSV type provider

How the CSV provider works

The philosophy behind type providers

Designing and implementing the CSV type provider

Design strategy

Inferring column types

Implementing the runtime and type provider

Implementing the Yahoo! Finance type provider

Getting company information using YQL

Implementing the type provider

Generating company names lazily

Reusing the CSV provider

Yahoo! Finance provider in action


About the author

Part 3 Developing complete systems

7. Developing rich user interfaces using the MVC pattern

Setting the scene

The trading application

Introducing the MVC pattern

Defining the model

Implementing the main trading view

Handling interactions in the controller

Gluing together the MVC pieces

Living in an asynchronous world

Making data binding safer

Are magic strings the only choice?

Introducing statically typed data binding

Handling nullable values in data binding

Handling conversions

Adding transformations


About the author

8. Asynchronous and agent-based programming

All about asynchronous workflows

What are asynchronous workflows?

Meet the agents

Extracting data from the world

The extract, transform, and load (ETL) model

Scheduling ETL work

Implementing ETL pipeline workers

Putting a system together

Introducing scalable systems

Building on event streams

Designing for scalability

Implementing the system

Going beyond simple agents


About the author

9. Creating games using XNA

Getting started

Defending Europe against incoming missiles

Understanding the execution flow of an XNA game

Structure of an XNA game in Visual Studio

Selecting the input device on the Press Start screen

Modeling and updating the game world

Rendering the game and the scoreboard

Rendering the game

Rendering the scoreboard

Performing I/O safely using async

Putting it all together


About the author

10. Building social web applications

Designing a social game


The UI

Client-server interaction


Implementing the contracts

The model

Requests and response objects

Implementing the server

Common utility functions

The data access layer

The game engine

The HTTP server

Implementing the client


Representing the game state

Utility functions

The app

Putting it together


About the author

Part 4 F# in the larger context

11. F# in the enterprise

Sample project

We’ll start with some data

Creating the basic service implementation

Creating a client wrapper for the service

Giving your application a user interface

Creating the basic Prism shell

Creating a Prism WPF plug-in module in F#

Making your service available throughout the application


About the author

12. Software quality

What software quality really means

Understanding requirements

Avoiding defects early with types

Holistic approach to testing

From exploratory to unit testing

Exploratory testing

Unit testing

Parameterized tests

Writing effective unit tests

Fuzz testing with FsCheck

Avoiding dependencies with test doubles

Acceptance testing

State of the art

Specifying behavior with TickSpec


About the author

Appendix. F# walkthrough: looking under the covers

From expressions to functions

Starting with expressions

Wrapping expressions in functions

Using functions as values

Constructing and destructing data

Representing composite values with tuples

Representing composite values with records

Representing choices with unions

Object-oriented programming: the good parts

From functions to interfaces

Implementing interfaces

Composition over inheritance


About the author


About the Technology

F# is an elegant, cross-platform, functional-first programming language. With F#, developers create consistent and predictable programs that are easier to test and reuse, simpler to parallelize, and less prone to bugs. The language, its tooling, and the functional programming style have proven effective in many application areas like secure financial engines, machine learning algorithms, scientific calculations, collaborative web applications, games, and more.

What's inside

  • Covers many subjects including:
    • Numerical computing
    • Data visualization
    • Business logic
    • Domain-specific languages
  • Practical solutions to real problems
  • Information-rich programming, including LINQ and F# type providers
  • Covers F# 3.1 and VS 2013

About the reader

Readers should have at least an introductory knowledge of the F# language.

About the authors

Tomas Petricek contributed to the development of the F# language at Microsoft Research. Phil Trelford is an early adopter of F# and one of its most vocal advocates. They are joined by F# experts Chris Ballard, Keith Battocchi, Colin Bull, Chao-Jen Chen, Yan Cui, Johann Deneux, Kit Eason, Evelina Gabasova, Dmitry Morozov, and Don Syme.

combo $49.99 pBook + eBook
eBook $39.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

Beautifully written by F# experts — take a deep dive without holding your breath.

Kostas Passadis, IPTO

Outstanding real-world examples that are sure to appeal to both the novice and expert.

Jeff Smith, ITT Education Services

I love the "Deep Dives" concept. This book is full of insights about how to apply the power of F# to real-world problems.

Dennis Sellinger, Géotech SARL