Functional Programming Is Awesome

November 27, 2017

They say functional programming lets you program with less bugs in less time, what’s more, your code would be easier to read! Sounds almost too good to be true? Well, today, in the first post of this tutorial series, I will give you an brief introduction to the awesome world of functional programming by comparing it with imperative style programming, and show you why functional programming is getting so popular these days.

Clearer, Cleaner Code with Functional Programming

even_numbers = []
for number in all_numbers:
  if number % 2 == 0
    even_numbers.append(number)

If you are a programmer, chances are you have coded in an imperative style like the example above before. In order to filter out all the even numbers in a list, it just seems “natural” for us to first create an empty list for the even numbers later to populate, then run through the list with a for loop, telling the computer exactly what to do in every situation.

But since the idea is to “filter out” the even numbers, how about writing some code like below?

even_numbers = filter(lambda number : number % 2 == 0, all_numbers)

Here, we just asked the computer to magically spit out a list called even_numbers, which consists of filtered out numbers from the original all_numbers list with a rule number % 2 == 0.

That is a classic example of solving a problem in functional style. The code through out the series will be mainly written in Python 3. Although you might not be familiar with the language, you should have noticed that the code in functional style seem to contain far less “steps” than the first one above. That is because functional programming is declarative, it’s more abstract - you describe what the computer should do for you with logic, not every single step of how the computer actually does its job (which is imperative). Doesn’t it just sound like a smarter thing to do? 😉

Of course, because of the underlying von Neumann architecture of modern computers on this planet, the functional style code we have written will still be decomposed to assembly language and executed as sequenced “imperative” micro-operations in the end, how we write our code just won’t matter for the computer. But remember, programming languages are for humans, not machines! When we try to talk with computers, some clever abstractions will always make out job easier.

Actually, typing less and writing more readable code is only part of the goodies bundle functional programming brings us! We will cover some other cool stuff later in the series. If done right, functional programming is like a breath of fresh air for your programming experience!

The Rise of FP

Actually, functional programming is hardly a new thing! The underlying theoretical framework of functional programming - Lambda calculus - was first introduced in as early as the 1930s.

In the 1950s, Lisp was developed at MIT. As one of the oldest family of programming languages, it is heavily functional-flavored and still very popular right now, and constantly growing - Clojure and Racket being some most popular Lisp dialects today.

When the computing power was scarce in the past, FP was once considered a wasteful practice because of its higher level of abstractions. Nowadays, most people don’t do manual memory management or any low level programming anymore; we even run programs above layers of VMs. Speed is not an issue in most areas now. Programmers today demand to be treated as humans and refuse to read convoluted code. Developer time becomes much more valuable than machine time in general.

Also, with multi thread processing and distributed computation on the rise, people are rediscovering another goodie functional programming promises us - immutability. This characteristic of FP allows us to design complex concurrent and parallel applications with less worries - there’s simply no way multiple threads will be trying to change the same data at once!

Coming Soon

It truly is a wonderful era for learning functional programming. In the next post of the series, I will walk you through some most fundamental concepts in functional programming - pure functions and immutability!