# Blog

## Functional Programming from Four Semesters of Computer Science in 5 Hours by Brian HoltPosted on August 27, 2020 in Algorithms, JavaScript by Matt Jennings

### Functional Programming Concepts

1. Avoid side effects, or keep functions pure.
2. Use Higher-Order Functions, which are functions that receives a function an as argument or returns the function at output.
3. Higher-Order Functions can be used on lists (or arrays) to transform lists of data. Function calls can be chained together to describe what we want to happen, rather than how.

### `.reduce()` for Arrays

Example code:

```const addTogether = list => {
return list.reduce((acc, num) => acc+num, 0);
};

const testList = [5,3,0,7,2,5,6,10,9]

const concatenateStringsWithSpaces = list => {
return list.reduce((acc, string) => acc + string + " ", "");
};

const testList = ['this', 'is', 'so', 'fun'];
console.log(concatenateStringsWithSpaces(testList).trim()); // 'this is so fun')

const squaresAndSubtracts = list => {
return list
.map( num => num*num )
.reduce( (accumulator, num) => accumulator - num );
};

const testList = [10, 5, 4, 2, 1];
console.log(squaresAndSubtracts(testList)); // 54```

### `.filter()` for Arrays

```const filterOutOdds = nums => nums.filter( num => num % 2 === 0);

const list = [1,5,7,2,6,3,5,4,10,50,51];
console.log(filterOutOdds(list)); // [2,6,4,10,50]```