RTM’d today: F# for C# Developers

670266.inddWe are happy to announce that F# for C# Developers, by Tao Liu (ISBN 9780735670266) has shipped to the printer! Please read on for preordering information, a link to the free sample chapters, the Contents at a glance, the foreword, and an excerpt from the Introduction.

 

Purchase from The Microsoft Press Store

click here

Preorder from Amazon

click here

Preorder from Barnes & Noble

click here

Preorder from Ada’s Technical Books and Café

click here

Download sample chapters from the Microsoft Download Center

click here

 

Contents at a Glance

Foreword
Introduction

PART I C# AND F#
CHAPTER 1 C# and F# Data Structures
CHAPTER 2 Using F# for Object-Oriented Programming
CHAPTER 3 F# and Design Patterns

PART II F#'S UNIQUE FEATURES
CHAPTER 4 Type Providers
CHAPTER 5 Write Your Own Type Provider
CHAPTER 6 Other Unique Features

PART III REAL-WORLD APPLICATIONS
CHAPTER 7 Portable Library and HTML/JavaScript
CHAPTER 8 Cloud and Service Programming with F#
CHAPTER 9 GPGPU with F#

Index

 

Foreword

People often ask, “What can F# do that C# cannot?” In this book, you will discover much
of what F# can do! You will see familiar things such as object programming and design
patterns. Further, you will also see powerful new things like pattern matching, piping,
first-class events, object expressions, options, tuples, records, discriminated unions, active
patterns, agents, computation expressions and, perhaps most distinctively, type providers.

However, we also need to ask the other question: “What can C# do that F# cannot?”
There is one important part to this answer that I will focus on here: C# can cause
NullReferenceExceptions. “What?” I hear you ask. “Does F# not have nulls?” Right! Perhaps
the most important thing the C# programmer needs to know about F# is that F# does not
use nulls in routine programming.

Let’s look at some evidence. People using F# at a major UK energy company did a
study of two similar ETL (Extract, Transform, Load) applications.1 Broadly speaking, the
applications were in the same zone in terms of functionality or, if anything, the F# application
implemented more features. The F# project had a very low bug rate, and its code
was 26 times smaller. The size difference is not only the result of language differences;
there are also differences in design methodology. The C# project is characterized by the
inappropriate overuse of elaborate object abstractions often seen in Java projects—for
example, elaborate and unnecessary class hierarchies.

Interestingly, the comparison records that the C# project had 3036 explicit null checks,
where a functionally similar F# project had 27, a reduction of 112 times in the total
number of null checks. The other statistics in the comparison shown are also compelling,
particularly the “defects since go live”: the F# code had zero defects since “go live,” and the
C# code had “too many.” These are not unrelated: nulls cause defects. In my opinion, the
lack of nulls in routine coding alone makes it worth switching your programming teams to
F# where possible.

In this book, you will learn many wonderful things about F#. But don’t lose sight of
the big picture: F# is about writing accurate, correct, efficient, interoperable code that
gets deployed on time in enterprise scenarios. It does this partly by removing the most
pernicious of evils: nulls. If you and your team embrace it, then, all else being equal, your
life will be simpler, happy, and more productive.

—Don Syme
F# Community Contributor

1 https://www.simontylercousins.net/journal/2013/2/22/does-the-language-you-choose-make-a-difference.html

 

Introduction (excerpt)

F# is a functional programming language from Microsoft. It is the first class language
shipped in Visual Studio. It has been applied successfully in several areas, such as in
the areas of financial software and web development. If you are a C# developer and
want to use functional programming to write concise code with fewer bugs, F# is the
right tool for you.

F# for C# Developers introduces, in an organized way, the F# language and several
applications. It starts from how F# can perform imperative and object-oriented
programming tasks and then moves on to covering unique F# features, such as type
providers. By introducing F# design patterns with a large number of samples, this
book not only delivers a basic introduction but also helps you apply F# in your daily
programming work.

In addition to covering core F# core features, I also discuss F# HTML5 development,
F# Azure development, and using general-purpose graphics processing units ( GPGPUs)
with F#. Beyond the explanatory content, each chapter includes examples and
downloadable sample projects you can explore for yourself.

Who Should Read This Book

I wrote this book to help existing C# developers understand the core concepts of
F# and help C# developers use F# in their daily work. It is especially useful for C#
programmers looking to write concise code for algorithm design, web development,
and cloud development. Although most readers will have no prior experience with
F#, the book is also useful for those familiar with earlier versions of F# and who are
interested in learning about the newest features.

You should have at least a minimal understanding of .NET development and
object-oriented programming concepts to get the most benefi t from this book. You
also should have a basic understanding of data structures and generic algorithms.
Experience in using C# is required as well.

Who Should Not Read This Book

This book is aimed at both experienced .NET C# developers who interested in
extending their knowledge in functional programming and beginners in F# who want
to understand F# and apply F# to their daily programming work. If you have no C#
programming experience, this book might be difficult for you.

Organization of This Book

This book is divided into three sections, each of which focuses on a different aspect.
Part I, “C# and F#,” introduce how to port your C# knowledge to F#. This section introduces
basic data structures and performing object-oriented implementations using F#.
Part II, “F#’s Unique Features,” introduces unique F# features and explains how to use
them in your daily programming work. Part III, “Real-World Applications,” introduces
several real-world applications, including web development, Azure cloud development,
and GPGPU.

Finding Your Best Starting Point in This Book

The various sections of F# for C# Developers cover a wide range of technologies.
Depending on your needs and your existing understanding, you might want to focus on
specific areas of the book. Use the following table to determine how best to proceed
through the book.

image

Most of the book’s chapters include hands-on samples that let you try out the
concepts just learned. No matter which sections you choose to focus on, be sure to
download and install the sample applications on your system.

Conventions and Features in This Book

This book presents information using conventions designed to make the information
readable and easy to follow:

■ Boxed elements with labels such as “Note” provide additional information or
alternative methods for completing a step successfully.

■ Text that you type (apart from code blocks) appears in bold. A plus sign (+)
between two key names means that you must press those keys at the same time.
For example, “Press Alt+Tab” means that you hold down the Alt key while you
press the Tab key.

System Requirements

You will need the following hardware and software to complete the practice exercises in
this book:
■ Windows 7 or Windows 8
■ Visual Studio 2012, any edition (multiple downloads might be required if you’re
using Express Edition products)
■ 1 GB (32 Bit) or 2 GBs (64 Bit) RAM
■ 3.5 GBs of available hard disk space
■ DirectX 9 capable video card running at 1024 x 768 or higher-resolution display
■ DVD-ROM drive (if installing Visual Studio from DVD)
■ Internet connection to download software or chapter examples
■ If you want to run the GPU code, you need an NVIDIA graphics card and you
need to download CUDA SDK from the NVIDIA web site.

Depending on your Windows configuration, you might require Local Administrator
rights to install or configure Visual Studio 2012.