Hvad er funktionel programmering? En nybegynder JavaScript-guide

JavaScript er et multi-paradigmesprog og kan skrives efter forskellige programmeringsparadigmer. Et programmeringsparadigme er i det væsentlige en masse regler, som du følger, når du skriver kode.

Disse paradigmer findes, fordi de løser problemer, som programmører står over for, og de har deres egne regler og instruktioner, der hjælper dig med at skrive bedre kode.

Hvert paradigme hjælper dig med at løse et specifikt problem. Så det er nyttigt at have et overblik over hver af dem. Vi dækker funktionel programmering her.

I slutningen af ​​denne artikel er der nogle ressourcer, du kan bruge til at gå længere, hvis du nød denne introduktion.

Der er også en GitHub-ordliste, der hjælper dig med at afkode noget af det jargon, som funktionel programmering bruger.

Endelig finder du et sted at gøre dine hænder beskidte kodning med praktiske eksempler og en GitHub repo fuld af ressourcer, du kan bruge til at lære mere. Så lad os dykke ind.

Deklarativ vs imperativ programmeringsparadigmer

Et eksempel på disse paradigmer, jeg talte om i starten, er objektorienteret programmering. En anden er funktionel programmering.

Så hvad er egentlig funktionel programmering?

Funktionel programmering er et subparadigme af det deklarative programmeringsparadigme med sine egne regler, der skal følges, når man skriver kode.

Hvad er det deklarative programmeringsparadigme?

Hvis du koder på et sprog, der følger det deklarative paradigme, skriver du kode, der specificerer, hvad du vil gøre uden at sige hvordan.

Et superenkelt eksempel på dette er enten SQL eller HTML:

SELECT * FROM customers

I ovenstående kodeeksempler implementerer du ikke SELECTeller hvordan man gengiver en div. Du fortæller bare computeren, hvad den skal gøre, uden hvordan .

Fra dette paradigme er der subparadigmer som funktionel programmering. Mere om det nedenfor.

Hvad er det absolut nødvendige programmeringsparadigme?

Hvis du koder på et sprog, der følger det tvingende / proceduremæssige paradigme, skriver du kode, der fortæller, hvordan man gør noget.

For eksempel, hvis du gør noget som nedenfor:

for (let i = 0; i < arr.length; i++) { increment += arr[i]; }

Du fortæller computeren nøjagtigt, hvad den skal gøre. Iterer gennem det kaldte array arrog derefter incrementhvert af elementerne i arrayet.

Deklarativ vs imperativ programmering

Du kan skrive JavaScript i det deklarative paradigme eller det imperative paradigme. Dette mener folk, når de siger, at det er et multi-paradigmesprog. Det er bare, at funktionel kode følger det deklarative paradigme .

Hvis det hjælper dig med at huske, ville et eksempel på en deklarativ kommando være at bede computeren om at lave dig en kop te (jeg er ligeglad med hvordan du gør det, bare tag mig lidt te).

Mens det er absolut nødvendigt, bliver du nødt til at sige:

  • Gå til køkkenet.
  • Hvis der er en kedel i rummet, og den har nok vand til en kop te, skal du tænde kedlen.
  • Hvis der er en kedel i rummet, og den ikke har nok vand til en kop te, skal du fylde kedlen med nok vand til en kop te og derefter tænde for kedlen.
  • Og så videre

Så hvad er funktionel programmering?

Så hvad betyder dette for funktionel kode?

Fordi det er et underparadigme fra det deklarative paradigme , påvirker dette den måde, du skriver funktionel kode på. Det fører generelt til mindre kode, fordi JavaScript allerede har mange af de indbyggede funktioner, du ofte har brug for. Dette er en af ​​grundene til, at folk kan lide funktionel kode.

Det giver dig også mulighed for at abstrahere meget (du behøver ikke at forstå i dybden, hvordan noget bliver gjort), du kalder bare en funktion, der gør det for dig.

Og hvad er de regler, der fører til funktionel kode?

Funktionel programmering kan simpelthen forklares ved at følge disse 2 love i din kode:

  1. Du arkitekterer din software ud fra rene, isolerede funktioner
  2. Du undgår mutabilitet og bivirkninger

Lad os grave i det.

1. Arkitekt din software ud fra rene, isolerede funktioner

Lad os starte i starten,

Funktionskode gør meget brug af et par ting:

Rene funktioner

Den samme input giver altid den samme output ( idempotens ) og har ingen bivirkninger.

En idempotent funktion er en, der, når du genanvender resultaterne til den funktion igen, ikke producerer et andet resultat.

/// Example of some Math.abs uses Math.abs('-1'); // 1 Math.abs(-1); // 1 Math.abs(null); // 0 Math.abs(Math.abs(Math.abs('-1'))); // Still returns 1 Math.abs(Math.abs(Math.abs(Math.abs('-1')))); // Still returns 1

Bivirkninger er, når din kode interagerer med (læser eller skriver til) ekstern mutabel tilstand.

Ekstern mutbar tilstand er bogstaveligt talt alt uden for funktionen, der ville ændre dataene i dit program. Indstille en funktion? Sæt en boolsk på et objekt? Slet egenskaber på et objekt? Alle ændringer i tilstand uden for din funktion.

function setAvailability(){ available = true; }

Isolerede funktioner

Der er ingen afhængighed af programmets tilstand, som inkluderer globale variabler, der kan ændres.

We will discuss this further, but anything that you need should be passed into the function as an argument. This makes your dependencies (things that the function needs to do its job) much clearer to see, and more discoverable.

Ok, so why do you do things this way?

I know this seems like lots of restrictions that make your code unnecessarily hard. But they aren't restrictions, they are guidelines that try to stop you from falling into patterns that commonly lead to bugs.

When you aren't changing your code execution, forking your code with if 's based on Boolean's state, being set by multiple places in your code, you make the code more predictable and it's easier to reason about what's happening.

When you follow the functional paradigm, you'll find that the execution order of your code doesn't matter as much.

This has quite a few benefits – one being, for example, that to replicate a bug you don't need to know exactly what each Boolean and Object's state was before you run your functions. As long as you have a call stack (you know what function is running/has run before you) it can replicate the bugs, and solve them more easily.

Reusability through Higher order functions

Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called first class functions.

In JavaScript, all functions are first class functions. Functions that have a first class status allow us to create higher order functions.

A higher order function is a function that either take a function as an argument, returns a function, or both! You can use higher order functions to stop repeating yourself in your code.

Something like this:

// Here's a non-functional example const ages = [12,32,32,53] for (var i=0; i < ages.length; i++) { finalAge += ages[i]; } // Here's a functional example const ages = [12,32,32,53] const totalAge = ages.reduce( function(firstAge, secondAge){ return firstAge + secondAge; }) 

The in-built JavaScript Array functions .map, .reduce, and .filter all accept a function. They are excellent examples of higher order functions, as they iterate over an array and call the function they received for each item in the array.

So you could do:

// Here's an example of each const array = [1, 2, 3]; const mappedArray = array.map(function(element){ return element + 1; }); // mappedArray is [2, 3, 4] const reduced = array.reduce(function(firstElement, secondElement){ return firstElement + secondElement; }); // reduced is 6 const filteredArray = array.filter(function(element){ return element !== 1; }); // filteredArray is [2, 3]

Passing the results of functions into other functions, or even passing the functions themselves, in is extremely common in functional code. I included this brief explanation because of how often it is used.

These functions are also often used because they don't change the underlying function (no state change) but operate on a copy of the array.

2. Avoid mutability and side-effects

The second rule is to avoid mutability – we touched on this briefly earlier, when we talked about limiting changes to external mutable state – and side effects.

But here we'll expand further. Basically, it boils down to this: don't change things! Once you've made it, it is immutable (unchanging over time).

var ages = [12,32,32,53] ages[1] = 12; // no! ages = []; // no! ages.push("2") // no!

If something has to change for your data structures, make changes to a copy.

const ages = [12,32,32,53] const newAges = ages.map(function (age){ if (age == 12) { return 20; } else { return age; } })

Can you see I made a copy with my necessary changes?

This element is repeated over and over again. Don't change state!

If we follow that rule, we will make heavy use of const so we know things wont change. But it has to go further than that. How about the below?

const changingObject = { willChange: 10 } changingObject.willChange = 10; // no! delete obj.willChange // no! 

The properties of changingObject should be locked down completely. const will only protect you from initializing over the variable.

const obj = Object.freeze({ cantChange: 'Locked' }) // The `freeze` function enforces immutability. obj.cantChange = 0 // Doesn't change the obj! delete obj.cantChange // Doesn't change the obj! obj.addProp = "Gotcha!" // Doesn't change the obj!

If we can't change the state of global variables, then we need to ensure:

  • We declare function arguments – any computation inside a function depends only on the arguments, and not on any global object or variable.
  • We don't alter a variable or object – create new variables and objects and return them if need be from a function.

Make your code referentially transparent

When you follow the rule of never changing state, your code becomes referentially transparent. That is, your function calls can be replaced with the values that they represent without affecting the result.

As a simple example of checking if your code is referentially transparent, look atthe below code snippet:

const greetAuthor = function(){ return 'Hi Kealan' }

You should be able to just swap that function call with the string it returns, and have no problems.

Functional programming with referentially transparent expressions makes you start to think about your code differently if you're used to object orientation.

But why?

Because instead of objects and mutable state in your code, you start to have pure functions, with no state change. You understand very clearly what you are expecting your function to return (as it never changes, when normally it might return different data types depending on state outside the function).

It can help you understand the flow better, understand what a function is doing just by skimming it, and be more rigorous with each function's responsibilities to come up with better decoupled systems.

You can learn more about referential transparency here.

Don't iterate

Hopefully, if you've paid attention so far, you see we aren't changing state. So just to be clear for loops go out the window:

for(let i = 0; i < arr.length; i++) { total += arr[i]; }

Because we are changing a variable's state there. Use the map higher order function instead.

More Features of Functional Programming

I hope at this point you have a good overview of what functional code is and isn't. But there's some final concepts used heavily in functional code that we have to cover.

In all the functional code I have read, these concepts and tools are used the most, and we have to cover them to get our foundational knowledge.

So here we go.

Recursion in Functional Programming

It's possible in JavaScript to call a function from the function itself.

So what we could always do:

function recurse(){ recurse(); }

The problem with this is that it isn't useful. It will run eventually until it crashes your browser. But the idea of recursion is a function calling itself from its function body. So let's see a more useful example:

function recurse(start, end){ if (start == end) { console.log(end) return; } else { console.log(start) return recurse(start+1, end) } } recurse(1, 10); // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

This code snippet will count from the start argument to the end argument. And it does so by calling its own function again.

So the order of this will look something like this:

Add a debugger inside the if blocks to follow this if it doesn't make sense to you. Recursion is one tool you can use to iterate in functional programming.

What makes the first example and the second example different? The second one has what we call "a base case". A base case lets the function eventually stop calling into itself infinitely. When start is equal to end we can stop recursing. As we know we have counted to the very end of our loop.

But each call of the functions is calling into its own function again, and adding on to the function argument.

The code example I just included for the counting example isn't a pure function. Why is that?

Because the console is state! And we logged string's to it.

This has been a brief introduction to recursion, but feel free to go here to learn more here.

Why use recursion?

Recursion allows us to stop mutating state variables, for one.

There are also certain data structures (tree structures) that are more efficient when solved with recursion. They generally require less code, so some coders like the readability of recursion.

Currying in Functional Programming

Currying is another tool used heavily in functional code. The arity of a function refers to how many arguments it receives.

// Let's talk arity function arity2(arg1, arg2){} // Function has an arity of 2 function arity0(){} // Function has an arity of 0 function arity2(arg1, arg2, arg3, arg4){} // Function has an arity of 4

Currying a function turns a function that has an arity of more than 1, to 1. It does this by returning an inner function to take the next argument. Here's an example:

function add(firstNum, secondNum){ return firstNum + secondNum; } // Lets curry this function function curryAdd(firstNum){ return function(secondNum){ return firstNum + secondNum; } }

Essentially, it restructures a function so it takes one argument, but it then returns another function to take the next argument, as many times as it needs to.

Why use currying?

The big benefit of currying is when you need to re-use the same function multiple times but only change one (or fewer) of the parameters. So you can save the first function call, something like this:

function curryAdd(firstNum){ return function(secondNum){ return firstNum + secondNum; } } let add10 = curryAdd(10); add10(2); // Returns 12 let add20 = curryAdd(20); add20(2); // Returns 22

Currying can also make your code easier to refactor. You don't have to change multiple places where you are passing in the wrong function arguments – just the one place, where you bound the first function call to the wrong argument.

It's also helpful if you can't supply all the arguments to a function at one time. You can just return the first function to call the inner function when you have all the arguments later.

Partial application in Functional Programming

Similarly, partial application means that you apply a few arguments to a function at a time and return another function that is applied to more arguments. Here's the best example I found from the MDN docs:

const module = { height: 42, getComputedHeight: function(height) { return this.height + height; } }; const unboundGetComputedHeight = module.getComputedHeight; console.log(unboundGetComputedHeight(32)); // The function gets invoked at the global scope // outputs: NaN // Outputs NaN as this.height is undefined (on scope of window) so does // undefined + 32 which returns NaN const boundGetComputedHeight = unboundGetComputedHeight.bind(module); console.log(boundGetComputedHeight(32)); // expected output: 74

bind is the best example of a partial application. Why?

Because we return an inner function that gets assigned to boundGetComputedHeight that gets called, with the this scope correctly set up and a new argument passed in later. We didn't assign all the arguments at once, but instead we returned a function to accept the rest of the arguments.

Why use partial application?

You can use partial application whenever you can't pass all your arguments at once, but can return functions from higher order functions to deal with the rest of the arguments.

Function composition in Functional Programming

The final topic that I think is fundamental to functional code is function composition.

Function composition allows us to take two or more functions and turn them into one function that does exactly what the two functions (or more) do.

// If we have these two functions function add10(num) { return num + 10; } function add100(num) { return num + 100; } // We can compose these two down to => function composed(num){ return add10(add100(num)); } composed(1) // Returns 111

You can take this further and create functions to compose any number of multiple arity functions together if you need that for your use case.

Why use function composition?

Composition allows you to structure your code out of re-usable functions, to stop repeating yourself. You can start to treat functions like small building blocks you can combine together to achieve a more complicated output.

These then become the "units" or the computation power in your programs. They're lots of small functions that work generically, all composed into larger functions to do the "real" work.

It's a powerful way of architecting your code, and keeps you from creating huge functions copied and pasted with tiny differences between them.

It can also help you test when your code is not tightly coupled. And it makes your code more reusable. You can just change the composition of your functions or add more tiny functions into the composition, rather than having all the code copied and pasted all over the codebase (for when you need it to do something similar but not quite the same as another function).

The example below is made trivial to help you understand, but I hope you see the power of function composition.

/// So here's an example where we have to copy and paste it function add50(num) { return num + 50; } // Ok. Now we need to add 30. But we still ALSO need elsewhere to add 50 still // So we need a new function function add30(num){ return num + 30; } // Ugh, business change again function add20(num){ return num + 20; } // Everytime we need to change the function ever so slightly. We need a new function //Let's use composition // Our small, reusable pure function function add10(num){ return num + 10; } function add50Composed(num){ return add10(add10(add10(add10(addNum(num))))); } function add30Composed(num){ return add10(add10(add10(num))); } function add20Composed(num){ return add10(add10(num)); }

Do you see how we composed new functions out of smaller, pure functions?

Conclusion

This article covered a lot. But I hope it has explained functional code simply, along with some of the repeating patterns you will see over and over again, in functional and even non-functional code.

Funktionel kode er ikke nødvendigvis den bedste, og heller ikke objektorienteret kode. Funktionel kode bruges generelt til mere matematiske baserede problemer som dataanalyse. Det er også meget nyttigt til realtidssystemer med høj tilgængelighed, som ting skrevet på Erlang (et funktionelt sprog). Men det afhænger virkelig af problem til problem.

Jeg sender mine artikler på Twitter. Hvis du nød denne artikel, kan du læse mere der.

Sådan lærer du mere

Start her med freeCodeCamps introduktion til funktionel programmering med JavaScript.

Se her efter nogle biblioteker, du kan inkludere og lege med, for virkelig at mestre funktionel programmering.

Læs denne gode oversigt over masser af funktionelle koncepter.

Endelig er her en fremragende jargong-busting ordliste over funktionelle termer.