1 Introducing functional programming

 

This chapter covers

  • Benefits and tenets of functional programming
  • Functional features of the C# language
  • Using records and pattern matching for type-driven programs

Functional programming (FP) is a programming paradigm: a different way of thinking about programs than the mainstream imperative paradigm you’re probably used to. For this reason, learning to think functionally is challenging but also very enriching. My ambition is that, after reading this book, you’ll never look at code with the same eyes as before!

The learning process can be a bumpy ride. You’re likely to go from frustration at concepts that seem obscure or useless to exhilaration when something clicks in your mind, and you’re able to replace a mess of imperative code with just a couple of lines of elegant, functional code. This chapter will address some questions you may have as you start on this journey. What exactly is functional programming? Why should I care? Can I code functionally in C#? Is it worth the effort?

1.1 What is this thing called functional programming?

What exactly is functional programming (FP)? At a high level, it’s a programming style that emphasizes functions while avoiding state mutation. This definition is already twofold as it includes two fundamental concepts:

  • Functions as first-class values
  • Avoiding state mutation

Let’s see what these mean.

1.1.1 Functions as first-class values

1.1.2 Avoiding state mutation

1.1.3 Writing programs with strong guarantees

1.2 How functional a language is C#?

1.2.1 The functional nature of LINQ

1.2.2 Shorthand syntax for coding functionally

1.2.3 Language support for tuples

1.2.4 Pattern matching and record types

1.3 What you will learn in this book

sitemap