# Functional Programming is unfamiliar territory for most

If you want everything to be familiar you will never learn anything new.

- Rich Hickey (Author of Clojure)

# Imperative

## Or like giving instructions to an idiot

``````const arr = [1, 2, 3, 4, 5];

const doubler = (arr) => {
for (let i=0; i<arr.length; i++) {
arr[i] = arr[i] * 2;
}
return arr;
};

const doubleArr = doubler(arr);

console.log(doubleArr); // [2, 4, 6, 8, 10]``````

# Object Oriented

``````class Foo {
constructor(arr) {
this._arr = arr;
}

getSum() {
let sum = 0;
for (let i=0; i<this._arr.length; i++) {
sum += this._arr[i];
}
return sum;
}
}

const arr = [1, 2, 3, 4, 5];
const foo = new Foo(arr);
transformer.getSum(); // 15``````

# Declarative

## WHAT should happen

``````SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE City = 'London'``````
``````INSERT INTO student (id, name, age)
VALUES (‘1’, ‘alan’, 28);``````
``````DELETE FROM student
WHERE name = ‘alan’;``````

# Functional

## Is a list of things you CAN'T do

• No Assignments
• No Side Effects
• No Mutating or changing state
• No While/For Loops

# Functional Programming is so called because a program consists entirely of functions.

-  John Hughes , Why Functional Programming Matters

# Why?

No matter what language you work in, programming in a functional style provides benefits. You should do it whenever it is convenient, and you should think hard about the decision when it isn't convenient.

- John Carmack, ID Software

# Doesn’t change data that exists outside the current function

``````// unfunctional function:

a = 0

const increment = () => {
return ++a;
};

increment();
increment();
increment();``````
``````// unfunctional function:

a = 0

const increment = () => {
return ++a;
};

increment(); // 1
increment(); // 2
increment(); // 3``````
``````// functional function:

const num = 0;

const increment = (a) => {
return a + 1;
};

increment(num);
increment(num);
increment(num);``````
``````// functional function:

const num = 0;

const increment = (a) => {
return a + 1;
};

increment(num); // 1
increment(num); // 1
increment(num); // 1``````

# Map

## Takes a collection of items and returns a new collection of items that have been changed

``````const arr = [1, 2, 3, 4, 5];

const doubler = (element) => {
return element * 2;
};

const doubleArr = arr.map(doubler);

console.log(doubleArr); // [2, 4, 6, 8, 10]``````

Docs: Map [MDN]

# Reduce

## Takes a collection of items. It returns a value that is created by combining the items.

``````const arr = [1, 2, 3, 4, 5];

const summarizer = (previous, current) => {
return previous + current;
};

const sum = arr.reduce(summarizer);

console.log(sum); // 15``````

Docs: Reduce [MDN]

# Feedback Welcome!

Please fill out this feedback form:
https://goo.gl/forms/Hsn6oonjyIl1yxCm1

By Joe Karlsson

# Functional Programming For People That Hate Math

Many of us have heard of Functional Programming, and all the benefits it is supposed to bring to our code. You may have also heard that it's really hard and kinda "mathy." You will learn how can it be applied to the real-world without have a Master degree in Mathematics.

• 1,347