Write programs there are many ways, could you write the program looks like a series of commands, this is the so-called “imperative programming”, or when you write a program to put the things stored in the object, and to interact with them, send messages back and forth, this is the “object-oriented programming”, but today I talk about functional programming, like just said the two, Functional programming is a style of code, and this is not a discussion of whether to add; Or whether {} comes after or below an expression, but rather how we direct programs to execute, which is technically called a programming paradigm. So why care?

Interesting function ✨

When we talk about the world of functional programming, everything is a function. The concept is very much like a mathematical concept, which is what the teacher said to us when we were in school:

A special relationship between values: each given input has a definite output.

This definition is very important, because it is the foundation of our project, called pure function, pure functions is such a kind of function, it only depends on its input, does not depend on anything outside, looking forward to your parameters passed, only to return to the output, will not affect the other places, for example, the following these functions, at first glance, what do you think is there a problem?

First version ❌

let age = 19

function getMyAge() {
  console.log(`I'm ${age} years old.`)
}

getMyAge(age)
age = 20
getMyAge(age)
Copy the code

Version 2 ✅

function getMyAge(age) {
  return `I'm ${age} years old.`
}

getMyAge(19)
getMyAge(20)
Copy the code

In the first version, the function looks for variables from an external scope, affecting the function itself to some extent, and the output in this case, ideally, returns only values, if you noticed. If we call this function with the same arguments (or even no arguments), we get different values. That can’t happen in pure functions. Now that you have a basic understanding of the benefits of functional programming, there are many more. Let’s take a look at its power 💪.

Side effects

A side effect is any interaction with the outside world that occurs during computation. This does not happen when using pure functions. Our code becomes more predictable because the result only depends on its output

variability

Variability means that things can change, which is not recommended in functional programming. When we have mutable data, its state cannot be changed after creation. If you want to make some changes, you need to create new values.

Mutable example

function changeFirstElem(array) {
  array[0] = 'Lose yourself to dance'
}

const daftPunkPopSongs = ['Instant Crush'.'Get Lucky'.'One More Time']
changeFirstElem(daftPunkPopSongs)
Copy the code

Immutable examples

function changeFirstElem(array) {
  const modifiedArray = ['Lose yourself to dance'. array]return modifiedArray
}

const daftPunkPopSongs = ['Instant Crush'.'Get Lucky'.'One More Time']
const modifiedArray = changeFirstElem(daftPunkPopSongs)
Copy the code

This is a very good thing because we’re making things safer and our code less bug-prone, which means it’s easier to test and debug our code. Since we need to know what the output looks like based on the parameters, if the output is wrong, we can be sure that our function is doing something wrong, not for some indefinite reason.

recursive

Recursion is a technique that allows us to solve a problem by breaking it down into smaller pieces, which helps us avoid side effects when we interact with the outside world.

function myCount(int i) {
  if(i >= 10) return 0
  else return i + myCount(i+1)
}
myCount(1);
Copy the code

For me, recursion makes code easier to read and cleaner, and I prefer to use an iterative approach in many cases.

Superheroes in functional programming 🧚♀️

In addition to recursion, we have three functions that help us with data, map-filter-reducer. In JS, functions can also be treated as values, so we can pass functions as arguments to other functions.

Map, given a set of data, you can pass in a function to Map each item in the array.

const numbers = [1.2.3];
const doubles = numbers.map(num= > num * 2) / / (2, 4, 6]
Copy the code

Filter, which takes a set of data, you can pass a conditional function and return a filtered subset of the data.

const numbers = [1.2.3];
const isGreaterThanOne = numbers.filter(num= > num > 1) / / [2, 3]
Copy the code

Finally, Reduce, given a set of data, you can process them into a single value.

const numbers = [1.2.3];
const mySum = numbers.reduce((accumulator, num) = > accumulator + num) / / 6
Copy the code

Conclusion 💃

I’ve already started learning functional programming, and these advantages have inspired me to start and keep reading more. Obviously functional programming has its drawbacks, but they don’t matter right now. You’ll use the material I left behind and have fun with functional programming!

books

Hackernoon – Understanding Functional Programming Professor Frisby’s Mostly Adequate Guide to Functional Programming Functional JavaScript Mini Book by Jichao Ouyang Pragmatic Function Javascript online book

BBS

Anjana Vankil – Functional Programming: What? Why? How? One of my favourites Anjana Vankil – Immutable data structures for functional JS Fun Fun Function Series

The original link

The beauty of Functional Programming