Functional programming has been around for thelast 60 years, but so far its always been a niche phenomenon.

Thats about to change.

Not only are languages likeJavaorPythonadopting more and more concepts from functional programming.

Here’s why developers are in love with functional programming

Newer languages like Haskell are going completely functional.

In simple terms, functional programming is all about building functions for immutable variables.

Because of its nature, functional programming is great for in-demand tasks such as data analysis and machine learning.

Article image

This doesnt mean that you should say goodbye to object-oriented programming and go completely functional instead.

Its all about killing side effects

40% off TNW Conference!

This might sound boring, but at the end of the day, its pretty insightful.

Article image

A function, naively stated, is a thing that transforms some input into some output.

Except that its not always that simple.

And that could be a problem.

Article image

While we were only using a simple example, in more complex programs these can causereal difficulties.

This is much better:

We havent really changed much.

The output is still [1,2], and everything else remains the same, too.

Article image

We have changed one thing, however: the code is now free of side effects.

And thats great news.

When you now look at the function declaration, you know exactly whats going on.

Woman sitting on couch with MacBook on her lap

And a function without side effects is a pure function.

A very simple definition of functional programming is this: writing a program only in pure functions.

Pure functions never modify variables, but only create new ones as an output.

Article image

you could findbetter examples, but it was about the basic principle here.)

The latter can make debugging and maintaining code a lot harder.

These are the first declarations that you might want to change if youre adopting functional programming.

Article image

What functional programming is not (only)

Map and reduce.

Loops are not a thing in functional programming.

Its not functional, either, because youre modifying global variables.

Man on leather couch with laptop

Instead, consider this:

This is fully functional.

Its faster because youre not iterating through many elements of an array.

Thatdoesnt meanthat all functional code uses map, reduce, and the likes.

It doesnt mean that you need functional programming to understand map and reduce, either.

Its just that when youre abstracting loops, these functions pop up rather a lot.

Lambda functions

When talking about the history of functional programming, many start with the invention of lambda functions.

But although lambdas are without doubt a cornerstone of functional programming, theyre not the root cause.

Lambda functions are tools that can be used to make a program functional.

But you’ve got the option to use lambdas in object-oriented programming, too.

Static typing

The example above isnt statically typed.

Yet it is functional.

It can be a nice addition, though.

It includes a magic argument, $_, which makes side effects one of its core features.

Perl does have its virtues, but I wouldnt try functional programming with it.

Java:I wish you good luck with writing functional code in Java.

Thats not to say that Java is bad.

Scala:This is an interesting one: Scalas goal is to unify object-oriented and functional programming.

This may make it easier for them to go fully functional in the years to come.

Python:Python actively encourages functional programming.

This is very much a la theZen of Python: explicit is better than implicit!

Clojure:According to its creator, Clojure is about 80% functional.

All values are immutable by default, just like you need them in functional programming.

However, you might get around that by using mutable-value wrappers around these immutable values.

When you open such a wrapper, the thing you get out is immutable again.

Haskell:This is one of the few languages that are purely functional and statically typed.

While this might seem like a time-drainer during development, it pays off big when youre debugging a program.

Its not as easy to learn as other languages, but its definitely worth the investment!

Big data is coming.

And its bringing a friend: functional programming.

In comparison to object-oriented programming, functional programming is still a niche phenomenon.

That makes perfect sense: functional programming is great for big databases, parallel programming, and machine learning.

And all these things have been booming over the last decade.

While object-oriented code has uncountable virtues, those of functional code, therefore, shouldnt be neglected.

This article was written by Ari Joury and was originally published onTowards Data Science.

it’s possible for you to read ithere.

Also tagged with