Functional Programming

For People That Hate Math

slides.com/joekarlsson | @joekarlsson1

I am
Joe Karlsson

Software Engineer

How's this going to work?

What have y'all heard about FP?

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)

Programming Paradigms

Imperative

HOW it should run

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

*WHAT should happen

Is a list of things you CAN'T do

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

What CAN I do?

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

  • More concise

  • More performant

  • Easy to reason*

  • Easy to debug

  • Co-exists very well with code written in other styles

  • Gives you more ways to solve problems.

No Side Effects

Doesn’t rely on data outside the current function

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

No For/While Loops

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]

Demo

  • FP describes WHAT should happen

  • No Assignments

  • No Varying Your Variables. (Immutable)

  • No While/For Loops

  • No Side Effects

  • No Mutating or changing state

How do you get better?

Additional Resources

Joe Karlsson

Feedback Welcome!

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

Made with Slides.com