Skip to content

Instantly share code, notes, and snippets.

@chiller
Created November 3, 2015 18:32
Show Gist options
  • Save chiller/55c895de9c2a6b6ff3d6 to your computer and use it in GitHub Desktop.
Save chiller/55c895de9c2a6b6ff3d6 to your computer and use it in GitHub Desktop.
Functional Programming at Founders And Coders

functional programming

Endre Galaczi

🐦  @galacziendre
βœ‰οΈ  galacziendre@gmail.com

functional programming - intro

functional programming style evaluation of mathematical functions avoid changing-state and mutable data

functional programming - first-class functions

var triple = function(n) {
    return n * 3;
}

functions can be passed as arguments

functional programming - higher-order functions

map, filter, reduce

var numbers = [1,2,3].map(triple)

instead of:

input = [1,2,3]
numbers = []
for(var i = 0; i<=numbers.length; i++){
    numbers.push(input[i])
 }

functional programming - pure functions - no mutation, no assignment

var a = 5;
a = a + 1

no mutating global state

function πŸ’© triple(n){

    times_called = times_called + 1;

    return n * 3;
}

functional programming - recursion

no mutation -> no loops no while no for

✨ recursion ✨

function count_to_a_chicken(n){
    if(n==0) return "πŸ” ";
    else return count_to_chicken(n-1);
}

tail call optimisation

stack overflow

factorial 0 = 1
factorial n = n * factorial n-1

factorial(10) =
10 * factorial(9) =
10 * (9 * factorial(8)) =
10 * (9 * (8 * factorial(7))) =
10 * (9 * (8 * (7 * factorial(6))) =
...
10 * (9 * (8 * (7 * (6 * (5 * (4 * (3 * (2 * 1))))))))

tail call optimisation

factorial 0 i = i
factorial n i = factorial (n-1) (i*n)

factorial(4,1)=
(factorial(3,4))=
((factorial(2,12)))=
(((factorial(1,24))))=
((((factorial(0,24)))))=
((((24))))=

es6 tco

functional programming - expressions over statements

encourages writing code using expressions rather than statements

int total = 0;
for(int i = 1; i<=10; i++){
    total = total + 1;
}

sum [1..10]

functional programming - composition

hatch = 🍳  -> πŸ”
clone = πŸ”  -> πŸ” πŸ”

superhatcher x = clone(clone(hatch(x)))
superhatcher = clone.clone.hatch

superhatcher 🍳   ->  πŸ” πŸ” πŸ” πŸ”

functional programming - composition

var triple = function(n) {
    return n * 3;
}
var numbers = [1,2,3].map(triple)


input = [1,2,3]
numbers = []
for(var i = 0; i<=numbers.length; i++){
    numbers.push(input[i])
}

programming is about making things simpler

"There are only two hard things in Computer Science:
    cache invalidation and naming things."

-- Phil Karlton

why is it suddenly so popular?

memory is cheap

cpu is growing in # cores not in GHz

functionally pure code is easier to understand

pure functions are easier to unit test

unit testing pure functions

var mistery_func = function(a, b) {

    //lol I don't know why we need this
    //but it breaks otherwise

    write_to_file(random_filename(), "πŸ’© ");
    return a * 2 + b * 5;
}

side effects need to be cleaned up or need to be mocked

do you unit test?

learn by example or from your own mistakes

tests as communication

Joel Test

Do you use source control? Can you make a build in one step? Do you make daily builds? Do you have a bug database? Do you fix bugs before writing new code? Do you have an up-to-date schedule? Do you have a spec? Do programmers have quiet working conditions? Do you use the best tools money can buy? Do you have testers? Do new candidates write code during their interview? Do you do hallway usability testing?

  • do you always write tests?
  • do you have mandatory code review?

Getting practical

Islands of functional purity

You will probably never end up programming in a purely functional language

Like in javascript you are not going to start using recursion everywhere

Even if you never work in a functional language professionally,
understanding functional programming will make you a better developer.

It will give you a new perspective on your code and programming in general.

A little bit of haskell

sum [1..12]

factorial 0 = 1
factorial n = n * factorial (n - 1)

    let right_triangles = [(x, y, z) |
         x**2 + y**2 = z**2,
         x <- [1..20],
         y <- [1..20],
         z <- [1..20] ]

Thanks for listening

Endre Galaczi

🐦  @galacziendre
βœ‰οΈ  galacziendre@gmail.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment