Functional Programming: Week 1

Learning to program with "functional" concepts is hard. The traditional imperative approach seems intuitive, but that is likely only because it is what we've been doing since the beginning of our programming lives. Functional programming is a paradigm shift from imperative programming. The Haskell FAQ says

Functional programming does require a change in perspective, which some programmers find hard. But Ericsson's experience in training programmers in Erlang is that most find the transition easy -- provided they take the training need seriously rather than assuming that they can "pick it up on the day".

Learning functional programming requires consistent and concentrated efforts.

With an imperative approach the focus is on algorithms and tracking changes; i.e., how things are done. This involves looping, checking conditions, calling object methods, and updating object data members.

With a functional approach the focus is on the result and data transformations required to achieve it; i.e., what is done. This involves calling pure functions to transform immutable data.

In my experience of following an imperative approach to program design for many years, a shift to a functional approach is mind bending. It hurts my head. I've only been seriously working on taking a functional approach for a week, but already it is getting a bit easier. Any my code is much clearer and maintainable.

C#, the language I primarily work with, is certainly not a "functional language" but has been garnering more and more functional aspects through the years: generics, type inference, LINQ, lambda functions, readonly fields; and now in C#7: powerful tuple types reminiscent of Python, pattern matching, more expression bodied members.

Although I've only decided this week to shift my designs toward functional programming, it certainly isn't the only exposure I've had to some of the concepts. Anyone with a bit of JavaScript experience has seen functions, as first class citizens, being used as parameters to other functions. The little Hadoop work that I've done was an shining example of how pure functions can be used with map and reduce operations to distribute and parallelize data processing. In the .NET ecosystem, lambda expressions and LINQ have been around since C# 3.0. LINQ itself was a mind bending experience when I first encountered it, and is a good example of functional concepts: creating a data transformation pipeline to express what is wanted rather than how to do it.

The places I've used for direction with functional programming so far: