Real-World Functional Programming with Examples in F# and C# cover
welcome to this free extract from
an online version of the Manning book.
to read more
or

About this Book

published book

If you are an existing .NET developer who knows object-oriented technologies and are wondering what this new “functional programming” movement is about and how you can benefit from it, this book is definitely for you.

It’s particularly tailored for .NET developers with working knowledge of object-oriented programming and C# 2.0. Of course, you don’t need to know either of these for functional programming in general, or even F# in particular. In fact, it’s more difficult to learn functional programming if you’re already used to thinking in an object-oriented manner, because many of the functional ideas will appear unfamiliar to you. We’ve written the book with this concern in mind, so we’ll often refer to your intuition and use comparisons between OOP and functional programming to explain particular topics.

If you’re an existing object-oriented programmer using other languages and tools (for example, Java, Python, or Ruby) with the ability to understand languages quickly, you can also benefit from this book. We’re using examples in C#, but many of them would look similar in other object-oriented languages. The C# 3.0 features that aren’t available in other languages are briefly explained, so you don’t have to worry about getting lost.

This book doesn’t focus on academic aspects of functional programming, but if you’re a computer science student taking a course on the topic, you can read this book to learn about the real-world uses of functional concepts.

What will this book give you?

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

  • Functional programming concepts. As you read through the book, you’ll learn a new way to think about problems. We’ll see how complex object-oriented design patterns become one simple concept in functional programming. Functional thinking can be used when programming in any language, so this will be useful regardless of the specific technology you work with.
  • Basic functional constructs in practice. Some of the constructs are now available in C# 3.0, so you’ll see many of the examples in familiar C# code next to a clean functional F# implementation. We’ll also explain recent features in C# 3.0 and how they relate to functional ideas. Understanding the concepts deeply will help you to get the most benefit from the new features.
  • Writing real-world F# code. Even though the book isn’t only about F#, it will teach you everything you need to know to start. We’ll explore areas where F# really shines, including asynchronous programming and creating composable libraries.

We’re not claiming this is the perfect book for all purposes: computing isn’t a one-size-fits-all field. It’s worth knowing what this book isn’t to avoid disappointment.

What won’t this book give you?

This hasn’t been written as a reference book. If you haven’t been exposed to functional programming, the best way to read the book is to start from the beginning and follow the order of the chapters. That’s the way we’ve assumed it will be read, so later chapters often refer to concepts explained earlier. If you open the book somewhere in the middle, you may find it hard to understand.

This book also isn’t a quick guide to F# programming. The only way to become a good F# programmer is to understand functional programming ideas. You could learn all the F# keywords and rewrite your C# code in F#, but it wouldn’t do you much good. If you want to write idiomatic F# code, you’ll need to learn a different way of thinking that is used in functional programming. That’s what you’ll get by reading this book. Once you’ve adapted to thinking in a functional style, there are other books which can help you learn F# in more depth.

Our primary goal is to write a book that can be used by professional programmers to write solutions to real-world business problems. However, that doesn’t mean we can offer you a ready-to-use solution for the specific problem you need to solve right now. Instead, we’ve focused on the relevant concepts and techniques. We’ve demonstrated those principles with many examples, but it isn’t possible to cover all the areas where F# and functional programming can be applied.

Roadmap

This book uses an iterative structure. In part 1 (chapters 1-4) we’ll explain a few aspects of the most important topics, so that you can see the motivation and understand what makes functional programming different. Part 2 (chapters 5-8) systematically discusses all the foundations of functional programming. In part 3 (chapters 9-12) we build on these foundations and talk about best practices for functional programming, optimization, and some advanced techniques that most of the functional programmers occasionally need. Part 4 (chapters 13-16) presents more complex examples showing how to use functional programming to develop larger real-world projects.

Chapter 1 discusses the reasons why functional concepts are becoming increasingly important. It gives examples of existing technologies that you may already know and that benefit from some aspects of functional programming. It also shows the first sample application in F#.

Chapter 2 introduces the concepts behind functional programming. Without showing any details and mostly using C#, it’ll help you understand how these concepts relate to each other and what they mean for the structure of program.

Chapter 3 finally shows some real functional code. It demonstrates some data types used in F# such as a tuple and a list. We’ll see how to work with the types in F#, but we’ll also implement them in C# to explain how they work. This chapter introduces the idea of using functions as values, which is essential for functional programming.

Chapter 4 shows our first real-world application implemented in F#. We’ll use various .NET and F# libraries to implement a program for drawing pie charts. You’ll also see how to efficiently use the tools F# provides during the development process.

Chapter 5 talks about values. Functional programs are written as calculations that take values as arguments and return values as results, so it is easy to see why we have to start the systematic review of functional features by looking at numerous kinds of values.

Chapter 6 describes the most common way of working with values, which is to use higher-order functions. Working with values directly often requires a lot of repetitive code, so this chapter shows how to design and implement reusable operations.

Chapter 7 turns the attention to architectural aspects. The structure of a functional application is determined by the data that it works with. We’ll use an application that manipulates and draws simple documents to demonstrate this important principle.

Chapter 8 focuses on the architecture of applications that need to dynamically change their behavior at runtime. This can be done using functions, so we’ll talk about them in detail and we’ll also explain related topics such as closures.

Chapter 9 shows how to mix object-oriented and functional styles in F#. It demonstrates how you can use functional features like immutability with object-oriented concepts such as encapsulation when writing functional .NET libraries.

Chapter 10 focuses on correctness and efficiency. We’ll see how to write functions that can process data sets of arbitrary size and how to write these functions efficiently. You’ll also learn how to optimize code using imperative constructs like arrays.

Chapter 11 talks about refactoring, testing and laziness. We’ll explain how functional programming makes it easier to understand and improve existing code. We’ll also look at unit testing, seeing how composability and strictness remove the need for some types of test.

Chapter 12 starts by showing how we can work with collections of data. We’ll introduce F# sequence expressions, which are designed for this purpose. You’ll also see that this isn’t a built-in feature unlike its closest counterpart in C#—it’s an example of a more general ability to change what code means.

Chapter 13 presents a common scenario when working with data in F#. It starts by downloading data using a public web service, then parses it into a structured format. Finally we see how to visualize interesting aspects using Excel.

Chapter 14 shows how to use functional concepts to build applications that are easy to parallelize. It demonstrates this using an image processing application and a simulation featuring animals and predators that hunt them.

Chapter 15 describes how to build declarative functional libraries. The chapter shows that well designed libraries can be elegantly composed. As an example, we’ll see how to create a library for creating animations and a library for representing financial contracts.

Chapter 16 shows how to build GUI applications and in general, programs driven by external events. Implementing control flow like this is quite difficult in other languages, so we’ll look at tricks that make it much easier in F#.

Typographical conventions

The book contains numerous code examples that are typeset using fixed-width font. Longer samples are presented in listings with a heading. Since the book mixes C# and F#, the heading in side-by-side listings also indicates the language used. When showing code in F#, we distinguish between two forms of listing. Code marked as “F#” is plain source code that can be compiled as a whole. Listings marked as “F# Interactive” present snippets in F# entered to an interactive shell. The output produced by the shell is typeset using italics. Bold fixed-width font is used to highlight all C# and F# keywords in all the listings.

Naming conventions

In this book, we’re mixing not only two languages, but also a functional programming tradition with the object-oriented tradition. Since we want to use the natural style in both languages, we have to follow different naming conventions in F# and C#.

In C# we follow the usual .NET style. In F#, we use this notation without exceptions when developing classes or when writing components that can be accessed from other .NET languages. When we show F# code that is only a private implementation, we follow the functional naming style. Most notably, we use camelCase for both variable and function names. This is the usual style in F#, because a function declaration is essentially the same thing as a variable declaration.

Occasionally, we use shorter names and abbreviations. There are two reasons for this. First, this style has been often used by functional programmers. With better IDEs there are fewer reasons to use this style, so we’ve tried to minimize its use. In some cases the shorter name is a common term in functional programming, so we’ve kept it. The second reason is that sometimes we present two samples side-by-side, which means that we have to use a more compact coding style. Otherwise, the naming mostly follows the .NET style with a few exceptions that are discussed in the text.

StyleCop and FxCop

If you’re familiar with tools for code analysis such as StyleCop and FxCop, you may be wondering whether the code in this book follows the rules required by these tools. We follow most—but not all—of the usual .NET conventions. If you run the code through these tools, you’ll get numerous warnings, again for two reasons.

  • The tools for code analysis were developed for object-oriented languages using the naming and style of the object-oriented tradition. As you’ll learn in this book, the functional world is different in many ways and we simply cannot follow all the object-oriented principles. The F# language is successful because it is very different from C# and Visual Basic. This isn’t just visible in the language keywords, but in the overall style of programming that it uses and in some ways also in naming conventions that make the code more succinct.
  • The limited space we have in the book. We use source code samples to demonstrate important ideas, so we didn’t want to include noise that isn’t important for the discussion, but would be required to make the code comply with conventions.

Source code downloads

The source code for the examples in the book is available online from the publisher’s website at http://www.manning.com/Real-WorldFunctionalProgramming and at a code repository created by the authors http://code.msdn.microsoft.com/realworldfp.

Author Online

The purchase of Real-World Functional Programming includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to http://www.manning.com/Real-WorldFunctionalProgramming. This page provides information about how to get on the forum once you’re registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It’s not a commitment to any specific amount of participation on the part of the authors, whose contribution to the book’s forum remains voluntary (and unpaid). We suggest you try asking them some challenging questions, lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

Other online resources

In addition to Manning’s website (http://www.manning.com/Real-WorldFunctionalProgramming), we have created a companion website for the book at http://functional-programming.net. It contains additional information that you may find useful, source code for the individual chapters, and material that didn’t fit in the book. The page also links to recent articles related to functional programming, so you can look at it to learn more about this topic.

If you’re interested in F#, you may also want to check out the official Microsoft’s Developer Center available at http://msdn.microsoft.com/fsharp. It contains the most recent information about the language as well as links to articles, videos, and other F# resources. If you want to ask a question about F# and be sure that you’ll get a competent answer, you can visit the F# community forums available at http://cs.hubfs.net.

Get Real-World Functional Programming with Examples in F# and C#
buy ebook for  $39.99 $27.99
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage