Preface - F# Deep Dives (2015)

F# Deep Dives (2015)


For the last two years, interest in F# and functional-first programming has been growing steadily. More than 10 books about F# are now on the market, more than one new user group has appeared somewhere in the world every month during the last year, and the number of members of and visitors to the F# Software Foundation has been increasing at a steady rate.

Many people become interested in F# because they hear about the benefits that functional-first programming gives you: the F# type system mostly removes the need for null checking and other invalid states; avoiding a mutable state and its declarative nature makes it easier to understand your programs; and the agent-based programming model simplifies concurrency. These are all nice facts about F#, but how do you put them into practice?

We provide the answer with this book. Rather than introducing F# language features using simple, toy examples, we worked with F# experts who have hands-on F# experience. We asked each of them to write a chapter based on a real scenario that they solved with F#. Each chapter is a case study documenting not just the source code written, but also the author’s approach to solving the problem, how it integrates with the broader ecosystem, and the business benefits gained from using F#.

This means the narrative of the book isn’t built along the technical aspects of the F# language. Instead, we focus on the business benefits you get from using F# in practice. Although we look at diverse domains ranging from game development to financial systems and social network analysis, there is still a common theme. The four key concepts you’ll see repeated over and over are time-to-market, taming complexity, correctness, and performance. How these concepts fit with actual business problems is covered in detail in chapter 1, which was coauthored by lead F# language designer Don Syme.

About this Book

Have you looked into F# briefly, found it interesting, and felt that your business could benefit from using it—but you aren’t sure how to best use F# in practice, how to approach problems from the functional-first perspective, and how to convince your colleagues that F# is the right choice? If you’re nodding while reading these words, then you’re holding the right book!

In this book, we’ve tried to answer many of the difficult questions that you have to answer when you learn F# and want to use it in practice. What does your business gain from adopting functional-first programming? What are some of the areas where people have succeeded with F#, and what is the best strategy for integrating F# with the rest of your ecosystem? And how do F# developers and architects think when they approach a problem?

These aren’t easy questions, and there isn’t a single answer. So rather than seeking just 1 answer, we collected 11 answers from different people who come from different backgrounds and different industries. If you’re working in a particular industry, then you’ll likely find a chapter that is close to what you’re doing, and you can start reading the book from there, focusing on the topic(s) that are close to you. That said, none of the chapters is specific to a single industry or a single problem. Each chapter has a more general takeaway point that is interesting even if you’re coming from elsewhere.

Although the chapters were written by different people and cover different topics, we tried to find a common narrative for the book. This link is provided by chapter 1, “Succeeding with Functional-First Languages in the Industry,” which is based on Don Syme’s talk from NDC 2013. As someone who has been in touch with many successful F# users since the first versions of F#, Don is the most qualified person to give the bigger picture. And you’ll see that the points discussed in chapter 1 keep reappearing in the later chapters.

What will this book give you?

If you’re still wondering whether this is the right book for you, here’s what you’ll get from reading it:

· Industrial case studies —Rather than focusing on toy examples to demonstrate language features, this book is based on real uses of F# in the industry. In other words, you won’t see another naïve Fibonacci calculation, but rather a calculation of a cumulative binomial distribution used in a real-world life expectancy model, with unit tests and C# integration.

· Functional-first thinking —When explaining the implementation, each chapter offers a bigger picture and describes not just the finished code, but also the considerations that motivated the solution. This means you’ll learn how experienced F# developers and architects approach the problem and how this differs from (and overlaps with) other methodologies, such as object-oriented and test-driven approaches.

· Business value —As developers, we often get carried away with interesting technical problems and forget that software is created to solve business problems and produce a unique business value. We tried to keep this in mind throughout the book. For more business-minded readers, this book explains what you gain by using F#. For more technical readers, we give you all you need to effectively explain the benefits of F# to your colleagues and managers.

Of course, no single book can be a great fit for all purposes, so it’s worth saying what this book isn’t, to avoid disappointment.

What won’t this book give you?

This book is not an F# language introduction. Although it includes an appendix that gives a quick overview of the most important F# language features, it’s probably not the first F# resource you should look at. You don’t need to be an F# expert to read this book, but some basic familiarity with the language is useful if you want to understand all the technical content of the book.

The book includes 11 different case studies, but you probably won’t find a ready-to-use solution to the problem you’re currently solving (unless you’re really lucky). We teach you how to use the functional-first approach to solve problems in the industry, and we demonstrate this using a number of examples from diverse domains, but in the end, you’ll have to do the work on your own.

If there is a single rule in computing that always holds, it’s the rule that there is no single rule in computing that always holds. Keep this in mind when reading this book and using the ideas described here!

How to read this book

Unfortunately, books these days still have to be organized in a sequential order, including this one. This is perhaps the best order to follow if you want to read the entire book, because it starts with more accessible chapters and continues to more advanced topics. But there are other options:

· By problem domain —Later chapters don’t generally rely on earlier chapters, so if you’re working in a specific industry (say, finance or gaming), you can start with the chapters that discuss topics that are closest to your domain.

· By programming theme —Similarly, you can look at the cross-cutting problems that interest you. For example, choose the chapters that talk about user-interface development, writing numerical calculations, testing, and enterprise or asynchronous and concurrent programming.

· As a technology overview —Finally, you can read parts of this book to get an overall idea about the business value that F# provides. If you’re not interested in all the technical details but want to know why and how F# is used, you can read chapter 1 and then read the first few pages of the other chapters. Each chapter starts with the business perspective, so you get a good idea of the reasons to choose F# for different problems and tasks.

To help you find what you’re interested in, let’s quickly go through the table of contents.


The book starts with an introductory chapter, followed by four parts that group the content by theme. It ends with a brief F# overview in an appendix:

· Chapter 1 looks at the business motivations behind using functional-first programming languages. What are the problems faced by businesses when developing software (especially analytical components)? How does F# help to solve those?

Part 1 contains easier-to-read chapters that serve as a great starting point to refresh your F# knowledge. It also demonstrates how F# makes it possible to write code that corresponds to the description of a problem domain:

· Chapter 2 presents a relatively simple numerical problem that arises when modeling life expectancies. It shows how you can easily encode numerical computations in F# and goes into interesting details about making the implementation practical.

· Chapter 3 explores the power of pattern matching for encoding the logic of your code. The example used throughout the chapter is a simple Markdown parser, but the chapter also highlights the benefits of modeling domain using functional types.

Part 2 focuses on implementing advanced analytical components for calculations and data processing:

· Chapter 4 looks at using F# for financial simulations using Monte Carlo methods. Because of the nature of the problem, this chapter is more mathematical than the others, but the key point is easy to see: F# makes it easy to turn mathematical models into code.

· Chapter 5 uses F# to understand social networks—specifically, Twitter. It’s a great example of end-to-end data analysis with F#, starting from data access, implementing algorithms such as Page Rank, and visualizing data using the D3 visualization library.

· Chapter 6 looks at writing type providers—a unique F# feature that makes it possible to integrate external data sources directly into the F# language. The chapter describes the implementation of a type provider for easy access to stock prices using Yahoo! Finance.

Part 3 discusses how to use F# for the development of complete systems:

· Chapter 7 is a myth buster. It shows not only that F# can be used for user interface programming, but also that using F#, you get additional safety and correctness that would be impossible in other languages. The chapter uses a simple trading application as an example.

· Chapter 8 discusses the development of scalable concurrent data processing using F# asynchronous workflows and the agent-based programming model. Along the way, it creates a simple agent-based framework and also an elegant computation expression for writing data-processing computations.

· Chapter 9 is about game development with F#. It incorporates topics that are important in pretty much any application: how to asynchronously handle user interactions and how to implement control-flow logic that is modeled using state machines.

· Chapter 10 is based on experience with developing the server-side part of social games using F#. It discusses the unique challenges of social gaming (like scalability) and develops a simple yet complete multiplayer farming game.

Part 4 describes how to integrate F# with the larger context:

· Chapter 11 takes a step back from the technical perspective and discusses how to succeed when introducing F# into the enterprise. It talks about using F# in a risk-free way and integrating F# components into larger C# solutions.

· Chapter 12 talks about one of the easiest ways of introducing F# into the enterprise: testing. It doesn’t just talk about unit tests, but instead discusses what should be tested, how to write useful tests, and how F# simplifies this task through a number of great libraries.

Finally, the appendix is a quick overview (and reminder) of key F# features. Rather than going through language features one by one, it tries to show the bigger picture—the focus on compositionality that exists in all aspects of the language.