📜 ⬆️ ⬇️

Functional JavaScript, Part 1: Introduction

Introduction


JavaScript is a powerful but misunderstood programming language. People like to say that it is an object-oriented programming language or is a functional language. Others like to say that it is not object-oriented or not a functional programming language. Some will say that it applies to both paradigms or to none of them - but, let's put it aside for the time being on this dispute.

Let's set the mission: to write in JavaScript, using the principles of functional programming as much as the language allows.

This series of posts means that you go on this journey with me. And, for a start, it is necessary to clarify some incorrect concepts concerning functional programming that may be present in your head.

Functional programming (strongly) misunderstood in the JS world


Obviously, there is a significant group of developers using the functional paradigm in JavaScript day after day. I would say that there is a large group of JavaScript developers who do not understand what it really means.

I think this is the result of the fact that most of the programming languages ​​used for server-side web development are rooted in C, and most will agree that it is not a functional programming language.
')
There are two points here. The first point can be demonstrated by the following example on jQuery, which is widely used:

$(".signup").click(function(event){ $("#signupModal").show(); event.preventDefault(); }); 

Hey, look at that. We have just executed the transfer of an anonymous function as an argument, also known in JavaScript under the ugly name callback function.
Some call it functional programming. Is it so? Not really!

This example is a demonstration of the key feature of functional programming: function as a parameter. On the other hand, this example also runs counter to almost any other of the paradigms of functional programming with which in general you can go against three lines of the example.
The second point is a little more insidious. While reading this, some trendy JS developers think to themselves:

Well! I already know everything about functional programming. I use Underscore.js in all my projects

Underscore.js is a fairly popular JavaScript library used everywhere. For example, let's say that I have a set of words and we need the corresponding set of the first two letters of each word. With Underscore.js, this is a fairly easy task:

 var firstTwoLetters = function(words){ return _.map(words, function(word){ return _.first(word, 2); }); }; 
You see! Check out this voodoo javascript. I use these wonderful functional utilities like _.map and _.first . What do you answer now IT, Leland? one

Although underlining and functions like _.map are useful functional paradigms, however, the way they are used together in this example looks ... verbose and hard for me. Do we really need all this?

If we start thinking about things a little more “functionally,” then, probably, taking the example above, we will get this:

 // ...   var firstTwoLetters = map(first(2)); 


If you think about it, you will see all the same information in one line as in the five lines above. words and word just options / placeholders. The real meat of logic is to combine the map function, the first function and the constant 2 in a meaningful way.

Some, looking at this example, may wonder: what else is there for the “drop of magic.” In the end, to insert any example with a “drop of magic” as in the example is like ... a kind of deception, eh?

Well, I'm going to devote a couple of the following posts, explaining this “little bit of magic”, so if you are intrigued, please continue.

This series of posts is designed to help others learn to borrow beauty from functional programming languages ​​in the context of JavaScript.

In the next post I’m going to talk about the various elements of JavasScript that are functional, just as they are not. With this knowledge, we will slowly put together the fundamental blocks of functional programming and how they look in JavaScript

Read more -> Part 2: What makes a language "functional"?

About errors and inaccuracies, please write here.

1 Posted by Leland Richardson

Source: https://habr.com/ru/post/229893/


All Articles