As software becomes more and more complex, it is more and more important to structure it well. Well-structured software is easy to write and to debug and provides a collection of modules that can be reused to reduce future programming costs. This all can be achieved through functional programming.
In my series of post on functional programming I will try to demonstrate the significance of functional programming, that will help C# programmers to exploit its advantages.
- Is C# Functional Language?
- Benefits Of Functional Programming
- What Are FP Functions / Pure Functions?
- How You Can Define Functions In C#
- Why Function Purity Matters?
- Further Reading
Is C# Functional Language?
We tend to see the introduction of more functional features with every new release of C# language, enabling a multiparadigm programming style in the language, for example:
C# 2.0 also allows programmers to pass and returns functions as values for higher-order functions using delegates, and has limited support for anonymous delegates.
C# 3.0 and 3.5 improved support anonymous functions for true closures.
LINQ can be considered C#’s own flavor of Haskell’s list comprehensions; Haskell is a statically typed, purely functional programming language with type inference and lazy evaluation; which somewhat LINQ and C# type inference represent.
Anonymous types look like an approximation of ML records. ML is a general-purpose functional programming language.
I wouldn’t necessarily consider some of those features mentioned above as exclusive to functional programming languages, but it’s pretty clear that the C# developers have taken a lot of inspiration from functional programming languages in the past few years.
For a more in-depth explanation see Is C#7 starting to look like a functional language?.
However, the adoption of functional programming in the C# community has been slow.
This adoption is slow as most of the books and articles explain functional techniques and concepts with examples from the domains of mathematics or computer science. However, most programmers work on business applications and this creates a domain gap.
Let’s see why Functional Programming matters and you should try to adopt it,
Benefits Of Functional Programming
Functional programming (FP) is more declarative in style.
We know for sure what a piece of code does and doesn’t do, thus we can change our code with more confidence.
The other major benefit is concurrency, which is easier to do with functional programming because a program written in the imperative style may work well in a single-threaded implementation but cause all sorts of bugs, like race condition, when concurrency comes in. Functional code offers much better guarantees in concurrent scenarios.
There is no hidden state (private fields or static fields) or any kind of dependency on the outside world. This fact alone is going to make testing much easier because we don’t have to worry about Mocking dependency or other hacks to prepare the functionality for the test.
In Functional Programming, by looking at functions we immediately receive a lot of information about what a function does just by looking at its signature. Sometimes that is all we need to know about a function and that’s far more clear and faster than the behavior methods in Object-oriented style.
To understand these benefits we must first understand what are “FP Functions” / “Pure Functions” in functional programming and why they matter?
What Are FP Functions / Pure Functions?
A Pure Function Is A Map Between Two Sets
In mathematics, and also in functional programming, a function is a map between two sets, respectively called the domain and codomain. That is, given an element from its domain, a function yields an element from its codomain.
In the above example our domain and codomain both are Integers i.e. they are range from -2,147,483,648 to 2,147,483,647. This is easy to write as the validation is already done by the language itself but this is not always the case when you write business logic. Let’s see a different kind of example:
In the above example the method has two possible outcomes actually three it can return a string, a null object, or throws an exception. And this will be the case for every function that operates on email. One way to improve this program is creating a data object that represents our domain. Let’s see how
In this implementation, Email still uses a string in its underlying representation, but the constructor ensures that Email can only be instantiated with a valid value.
The Email type is being created precisely to represent the domain of the GetDomains function, which can now be rewritten as follows:
This new implementation has several advantages. You’re guaranteeing that only valid values can be given; GetDomains no longer causes runtime errors; and the concern of validating the email value is captured in the constructor of the Email type, removing the need for duplicating validation wherever an Email is processed.
The Output Of A Pure Function Is Determined Exclusively By Its Input
The value that a function yield is determined exclusively by its input. You’ll see that this isn’t always the case with functions in programming. Let’s see why
Is the IsMinor method pure? The result of IsMinor will depend on the current date, so, clearly, the answer is no! What kind of side effect are we facing here? It’s I/O: DateTime.Now queries the system clock, which is not in the context of the program.
Functions that perform I/O are difficult to test. For example, the following test passes as I’m writing this, but it will start to fail on the 16th of December, 2019:
You can address this issue in the following manner:
Now the implementation of IsMinor is pure (because today is not mutable). You’ve effectively pushed the side effect of reading the current date outwards.
However, still, our method is dependent on a hidden field. The programming constructs we use to represent functions all have access to a “context”: an instance method has access to instance fields.
As we discussed Pure functions closely resemble mathematical functions: they do nothing other than computing an output value based on their input values. They never mutate global state—“Global” here means any state that’s visible outside of the function’s scope.
A private instance field is considered global because it’s visible from all methods within the class.
So we can even further refactor our code to attend true purity.
Now we can write the test case as follows:
And client can use the code in following manner:
Domain And Codomain Constitute A Function’s Interface
The types for the domain and codomain constitute a function’s interface, also called its type or signature. You can think of this as a contract: a function signature declares that, given an element from the domain, it will yield an element from the codomain.
In the above example, we defined this using return type of method and type for parameters
Here both our domain and codomain are defined as int type.
Here our domain is define by a custom type Email and its codamain is a .NET build in type string.
Here our domain represents .NET
DateTime type while codomain will always be a boolean value true or false.
How You Can Define Functions In C#
There are several language constructs in C# that you can use to represent functions:
Methods are the most common and idiomatic representation for functions in C#. For example, the System.Math class includes methods representing many common mathematical functions.
Methods can represent functions, but they also fit into the object-oriented paradigm—they can be used to implement interfaces, they can be overloaded, and so on.
The constructs that really enable you to program in a functional style are delegates and lambda expressions.
Delegates are type-safe function pointers. Type-safe here means that a delegate is strongly typed: the types of the input and output values of the function are known at compile-time, and consistency is enforced by the compiler.
The Func And Action Delegates
The .NET framework includes a couple of delegate “families” that can represent pretty much any function type:
Func<R>represents a function that takes no arguments and returns a result of type R.
Func<T1, R>represents a function that takes an argument of type T1 and returns a result of type R.
Func<T1, T2, R>represents a function that takes a T1 and a T2 and returns an R.
And so on.
Since the introduction of Func, it has become rare to use custom delegates. For example, instead
of declaring a custom delegate like this,
you can just use the type:
The type of Greeter in the preceding example is equivalent to, or “compatible with,”
Func<Person,Greeting>. In both cases, it’s a function that takes a Person and returns a Greeting.
There’s a similar delegate family to represent actions—functions that have no return value, such as void methods:
Actionrepresents an action with no input arguments.
Action<T1>represents an action with an input argument of type T1.
Action<T1, T2>and so on represent an action with several input arguments.
The evolution of .NET has been away from custom delegates, in favor of the more general Func and Action delegates.
Lambda expressions, called lambdas for short, are used to declare a function inline. For example, sorting a list of numbers alphabetically can be done with a lambda like so.
If your function is short and you don’t need to reuse it elsewhere, lambdas offer the most
attractive notation. Also notice that in the preceding example, the compiler not only infers the types of x and y to be int, it also converts the lambda to the delegate type
Comparison<int> expected by the Sort method, given that the provided lambda is compatible with this type.
Dictionaries are fittingly also called maps (or hashtables); they’re data structures that provide a very direct representation of a function. They literally contain the association of keys (elements from the domain) to values (the corresponding elements from the codomain).
We normally think of dictionaries as data, so it’s enriching to change perspectives for a moment and consider them as functions. Dictionaries are appropriate for representing functions that are completely arbitrary, where the mappings can’t be computed but must be stored exhaustively.
Why Function Purity Matters?
The deterministic nature of Pure functions / FP functions (that is, the fact that they always return the same output for the same input) has some interesting consequences.
Pure functions are easy to test and to reason about. Furthermore, the fact that outputs only depend on inputs means that the order of evaluation isn’t important. Whether you evaluate the result of a function now or later, the result will not change.
This means that the parts of your program that consist entirely of pure functions can be optimized in a number of ways using techniques such as Parallelization, Lazy evaluation, Memoization. Using these techniques with impure functions can lead to nasty bugs. For these reasons, FP advocates that pure functions should be preferred whenever possible.
Let’s see what happens if we naively apply parallelization with the impure function:
Let’s say you want to format a list of strings, like below one, as a numbered list; and you want to use the parallel features of .NET along with it.
To do this, you’ll write a program something like this,
AsParallel() enables parallelization of a query. This method binds the query to PLINQ. PLINQ can achieve significant performance improvements over legacy code for certain kinds of queries, often just by adding the AsParallel query operation to the data source.
For more information, see Parallel LINQ (PLINQ).
Because the line no 4 in the above program increments the counter variable, and the parallel version will have multiple threads reading and updating the counter. As is well known, ++ is not an atomic operation, and because there’s no locking in place, we’ll lose some of the updates and end up with an incorrect result.
If you test this approach with a large enough input list, you’ll get a result like this:
You can see that the numbering is improper after the 13th item.
This will look pretty familiar if you have some multithreading experience. Because multiple processes are reading and writing to the counter at the same time, some of the updates are lost.
Now, the thing is
Here, our select method violates the pure function rule: “The output of a pure function is determined exclusively by its input”.
As our select lambda expression depends on an external closed-over variable count it makes our lambda function impure.
You probably know that this could be fixed by using a lock or the Interlocked class when incrementing the counter. But locking is an imperative construct that we’d rather avoid when coding functionally.
One possible way to avoid the pitfalls of concurrent updates is to remove the problem at the source: don’t use the shared state, to begin with. How this can be done will vary with each scenario,
but I’ll show you a solution for the current scenario that will enable us to format the list in parallel.
What if instead of updating a running counter, you generate a list of all the counter values you need, and then pair items from the given list with items from the list of counters?
For the list of integers, you can use Range, a convenience method on Enumerable. And, the operation of pairing two parallel lists is a common operation in FP, and it’s called Zip.
Using Range and Zip, you can rewrite the list as follows.
In this case, ParallelEnumerable does all the heavy lifting, and you can easily resolve the problem by reducing this specific scenario to the more common scenario of zipping two parallel sequences—a scenario common enough that it’s addressed in the framework.
In this scenario, it was possible to enable parallel execution by removing state updates altogether, but this isn’t always the case, nor is it always this easy. But the ideas you’ve seen so far already put you in a better position when tackling issues related to parallelism, and more generally concurrency.
That’s all for now, join me in the next article by subscribing to my newsletter using the form below.
Functional Programming Concepts Applied Using C# by Jorge Yañez - There has been a lot of fuzz in recent years about functional programming. You might be interested but it can be hard to apply it to your everyday work. This post aims to give you a cheatsheet with functional concepts and techniques used in the .NET framework.
What pure functional programming is all about: Part 1 - In this post you will look at a little language theory, type theory, and implementation and practice of pure functional programming. This post also discuss correctness, architecture, and performance of pure programs.
Functional Programming in C#: Map, Filter, and Reduce Your Way to Clean Code - You can do functional programming in C#. Learn C# Map, Filter, and Reduce in order to help you write cleaner code that’s easier to reason about.
Functional Programming in C#: How to write better C# code - Functional Programming in C# teaches you to apply functional thinking to real-world problems using the C# language. The book, with its many practical examples, is written for proficient C# programmers with no prior FP experience. It will give you an awesome new perspective.
Real-World Functional Programming: With Examples in F# and C# - Real-World Functional Programming is a unique tutorial that explores thefunctional programming model through the F# and C# languages. The clearlypresented ideas and examples teach readers how functional programming differsfrom other approaches. It explains how ideas look in F#-a functionallanguage-as well as how they can be successfully used to solve programmingproblems in C#. Readers build on what they know about .NET and learn wherea functional approach makes the most sense and how to apply it effectively inthose cases.
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox