Functional Programming in .NET book - An update
Recently, I announced on my blog that I’m working on a book for Manning called Real world Functional Programming in .NET. The goal of the book is to explain the most interesting and useful ideas of functional programming to a real world C# developer. I'm writing this book, because I believe that functional programming is becoming increasingly important. Here is a couple of reasons why you should have this book on your bookshelf:
- Ideas behind C# 3.0 and LINQ - these main-stream technologies are inspired by functional programming and the new C# 3.0 features give us definitely much more than just a new way to query databases. The book explains the ideas behind these features and shows how to use them more efficiently in a wider range of scenarios.
- Learning the F# language - F# is becoming a first-class citizen in the Visual Studio family of languages, which alone would be a good reason for learning it! Even if you're not going to use it for your next large .NET project, you'll find it useful for quick prototyping of ideas and testing how .NET libraries work thanks to the great interactive tools.
- Real world examples - the book includes a large set of real-world examples that show how to develop real .NET applications in a functional way - both in F# and C#. Among other things, the examples show how to utilize multi-core CPUs, how to better obtain and process data and how to implement animations and GUI applications in a functional way.
To summarize, the goal of the book is to explain the key ideas of functional programming to real world .NET developers in a way they can benefit from them. It actually very much follows my personal experience – I started seriously looking at functional programming in the F# language after using primarily C# for quite a long time. Anyway, you can read more about the book in my previous post, on the book web site or on the official Manning book page. The book is available via the MEAP (Manning Early Access Program) and if you want to get a better idea what is the book about, you can read the first chapter for free.
It is more than a month since I posted the announcement, so I decided to write a brief update. Manning is making the book available via the MEAP program, which means that if you can buy it right now and you’ll receive an updated electronic version every time a couple of chapters is finished. Once the whole book is completed, you'll get the final version (either electronic or both electronic and printed). Last time I wrote about the book, there were first 4 chapters available. If you buy the book today, you’ll get first 9 chapters. Chapters 8 and 9 were added just this week!
About two weeks ago, the MEAP version of the book was first in the list of Manning bestsellers (even before books on topics like ASP.NET "MVC" and Silverlight, which was a very nice surprise for me)! Click on the image to see the full-size screenshot:
The most interesting thing for me about writing the book is that I really had to shape my ideas very clearly and find a good way to organize all the topics. One point of view that I found particularly useful (and it reflects how I write code in F#) is that F# gives you a way to start with a very simple, easy to write and clear code and later turn it into a robust .NET code. At the beginning you can write your code just as a script for F# interactive using only the basic functional features. Thanks to the type inference the code is also very succinct. In the later phase you can use features like abstract interface types and object expressions to organize the code and make it easily accessible for example from C#. The nice thing is that the transition is very smooth, so you don't have to rewrite almost any code.
Since many of the functional features are now available in C# 3.0, it is possible to apply some of these ideas to C# too. In particular, we can simplify many object-oriented constructs (including some well known design patterns) and also use many effective functional techniques. This way of thinking is very suitable for a book, because we can start by looking at simple and clear ideas and then learn more sophisticated techniques and language features. The chapters that are currently written follow this organization. Here is a brief overview of the content of every chapter:
Part I. Introduction
To many of the readers, functional programming is a completely new idea. The basic principles of functional programming are also quite different to what you may be used to. The first part of the book (containing chapters 1 - 4) serves as an introduction and touches lightly some of the ideas that will later become essential.
- Chapter 1: Thinking differently about problems starts with a little section about the long history of functional programming
(By the way, would you believe that the first functional programming language celebrated its 50 years in 2008?).
As a next thing, the chapter discusses some important reasons why functional concepts are important nowadays
and shows successful real world applications of these concepts. The largest part is a tasting of what you'll
find later in the book and finally, it contains a section showing the first example application in F#.
- The chapter is available for free from the Manning book page.
- Chapter 2: Functional concepts and programming languages introduces the concepts behind functional programming. As I already mentioned many of the functional concepts are different then what you might be used to, so this chapter gives a brief overview (without any details) showing how functional concepts relate and what they mean for the program structure.
- Chapter 3: Meet tuples, lists and functions in F# and C# finally shows some real functional code. In this chapter, we use both C# and F# when writing any code, which makes it easier to understand what the functional program does. It demonstrates some basic data types used in F# (such as tuples and lists) and also shows how they can be implemented in C#. This chapter also introduces the idea of using functions as values, which is one of the key functional concepts.
- Chapter 4: Exploring F# and .NET libraries by example presents the first real world application implemented in F# and shows how to use various .NET and F# libraries from the F# language. We look at an application that loads data from a file and presents them in a graphical form using a pie chart. This chapter finishes the introductory part by presenting the F# language by example, using only basic features from Chapter 3.
Part II. Core functional techniques
The second part starts describing the "iterative development style" enabled by functional programming in F#. It discusses elementary ways used to construct functional programs. These give us a very clear and concise way to think about problems. Finally, in the later chapters it talks about functional applications from a higher perspective focusing more on architectural acpects.
- Chapter 5: Creating and using common functional values - in functional programming, applications are described more like calculations that take some values as arguments and return a value as a result. This chapter explains what values actually are and talks about usual ways to construct them. This includes representing multiple values, alternative values and also functions (which are values too).
- Chapter 6: Working with values using high-order functions introduces better way for working with basic values from Chapter 5. Instead of manipulating with them directly, we can factor out the changing part of some common operation and then use the single function for many varying tasks. This talks mainly about using functions as arguments, which is also very important for C# 3.0, which supports lambda functions.
- Chapter 7: Designing data-centric programs - when designing an application in the functional style, we start by thinking about the data that it works with. This principle is very useful and clear independently of the language that you're actually using. In this chapter, we demonstrate this principle and also show how it relates to some well-known structural design patterns.
- Chapter 8: Designing behavior-centric programs - not all applications are primarily focused at working with data as described in Chapter 7. Here we talk about the second types of applications. Using functional terminology, this chapter talks about functions in detail. In the object oriented terminology, we look at some behavioral design patterns like Strategy or Command.
This section focuses on more advanced concepts that are used later in the "iterative development style". Once the application or a library grows larger, we want to use various .NET facilities to organize it intuitively, expose it to other .NET languages and document it. If the library is intended only for F# then we can leverage of some advanced F# features. Finally, we also want to make the code more efficient by using general functional and F# specific optimization techniques.
- Chapter 9: Turning values into F# object types with members shows how to mix object-oriented and functional style of programming in F#. It shows how the functional concepts (like immutability) can be used well together with object oriented features like encapsulation and how the conceptually simple ideas from Chapter 7 and 8 can be wrapped into real-world .NET code.
- Chapter 10: Efficiency of data structures, tail-recursion and continuations - in the earlier chapters we talked about recursion and data structures such as lists or trees. In this chapter, we look how to use them efficiently. This means how to avoid stack overflow errors when writing functional code and how to write fast code that processes lists and other collections.
These are all the chapters that I wrote already (Chapter 10 will be added to the MEAP program in the next update). I'll definitely blog about more chapters as I'll write them, so stay tuned for another update!
Looking for your feedback!
The book is still "work in progress", so I'm very glad to receive any feedback. I already got many useful comments and suggestions via the book forum at Manning, from the reviewers of the first third of the book and also from my colleagues and friends. However, this is probably the first book about functional programming that aims to a very wide audience of .NET programmers, requiring only advanced C# background, so any comments are more than welcome! You can post them to the forum mentioned above as well as directly to me at email@example.com.
- Real-world Functional Programming in .NET - book homepage
- Manning: Real-world Functional Programming in .NET - book page at Manning.Com
- Forum dedicated to the book - at Manning.Com