Using .map() and .filter() Does Not Functional Programming Make

For some unknown reason recently, treating all data like a stream has become trendy, and people have started to call this “functional programming”. This is happening not just in one language. It’s for sure going on in JavaScript and in Java. I’ve seen those. I’d bet it’s happening in other languages too.

Let me explain what I mean in case you are unfamiliar with this trend.

Here is a particularly pertinent article on the topic: Basically the idea is that if you have an array or various other data types, then you can adjust, filter, or otherwise get new data out of that by applying functions to it that do not mutate the data but instead return the result without mutating the input object. These functions are usually named .map, .filter, .reduce and the like. And every kind of data is a stream now for some reason.

The filter method, for example, returns a newly created array only containing the elements that pass the filter test function, and the filter test function gets passed in as an anonymous function or a lambda expression.

In Java, this style became very popular around the time of Java 8, when people started using streams, and they suddenly had lambda expressions. Before lambdas, Java was a nightmare.

It’s kinda cool, and useful in some cases I guess, but I see people suddenly using this coding style for every damn thing, and I just don’t get it. Not everything needs to be written to look like you are handling a stream type. And when you do write code in this fashion, that is not functional programming. The one and perhaps only thing it has in common with functional programming is that these functions don’t mutate the data.

Functional programming is a complex and, in my opinion, difficult programming paradigm. It sounds like it would be really fun to learn, but learning it has never yet proved essential to my career path. And JavaScript is not even well designed for any attempt at doing real functional programming because JavaScript interpreters tend to have such a small max on the recursion stack. Doing any kind of deep recursion in JavaScript will quickly cause an overflow error due to the recursion.

Haskell and Lisp are two languages that support for-real functional programming. I’m not a master of either. I just know enough of it to know that using the .map method is not it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s