Welcome to the talk!
=<< What's up with that Title? >>=
It’s functions, pure functions! Pure functions and their values, PURE FUNCTIONS FOREVER AND FOREVER AND FOREVER A HUNDRED YEARS monad... some... things... Me and monads runnin' around and...applicative functor time... a- all day long forever... All a - a hundred days applicatives and functors! Forever a hundred times.... OVER and over monad transformers... adventures dot com... W W W dot at lambda calculus and category theory dot com w..w..w... function composition adventures... Ah- hundred years… Every minute referential... transparency dot com.... w w w a hundred times... composition dot com...
Fig 1.1 What most people thing a typical functional programming advocate looks like
=<< Obligatory Quote >>=
=<< Obligatory Quote >>=
If you wish to build a ship, do not divide the men into teams and send them to the forest to cut wood. Instead, teach them to long for the vast and endless sea.
=<< Obligatory Quote >>=
If you wish to build a ship, do not divide the men into teams and send them to the forest to cut wood. Instead, teach them to long for the vast and endless sea.

- Some French Guy. Idk. Who cares, I'm not cultured. I'm Australian.
=<< What is this Talk About? >>=
=<< What is this Talk About? >>=
Perspective
=<< What is Functional Programming? >>=
Programming with functions.
=<< What is a Function? >>=
A mapping between sets that associates every element of the first set to exactly one element of the second set.
=<< Huh? >>=

Why would you wanna do that?!

=<< Huh? >>=

Why would you wanna do that?!

  • Learning is good
  • Brain plasticity
  • Greater perspective
=<< Paradigms >>=
Paradigms
=<< Paradigms >>=
Declarative
Functional
  • Haskell
  • Elm
  • Elixir
  • Clojure
Imperative
Procedural
  • PHP
  • C
  • Java
  • DamoScript
=<< Gramatical Mood >>=
Declarative A sentence which expresses a statement of fact.
  • He runs.
  • I like climbing.
  • Ice is cold.
  • Mathieu is shredded.
Imperative A sentence which expresses instructions, or requests.
  • Shut the door.
  • Don't eat my burger.
  • Let's go to the pub.
  • Don't make eye contact with Mathieu.
=<< Gramatical Mood >>=
Declarative This is this.
Imperative Do this.
=<< Big Brain Moment >>=
Programming languages ARE human languages.
=<< Why is declaritive mood good? >>=
=<< Why is declaritive mood good? >>=
Atemporality
=<< Why is declaritive mood good? >>=
Independant of or unaffected by time.
=<< Why is declaritive mood good? >>=
Declarative programs express what they actually do Leaving more "brain space" to focus on solving the problem.
=<< To Recap >>=

Why would you wanna do that?

  • Because sometimes there is such a thing as too much freedom
  • Functional programming is awesome because it stops you shooting yourself in the foot
  • "Constraints liberate, liberties constrain"
=<< Some Code (Finally) >>=
=<< Some Code (Finally) >>=
Enough of this philosophy and linguistics mumbo jumbo, already!
=<< Some Code (Finally) >>=

The Great Dividers

Consider:

x = x + 1;
and:
function repeat(x) {
    return x + repeat(x);
}

OK, so what?

In the declaritive paradigm, this code hangs while this code runs. In the imperative paradigm this code hangs, while this code runs.
=<< Some Code (Finally) >>=
x = x + 1;
Declarative "x is the same as itself plus one."




Complete nonsense!
Imperative "Evaluate the thing on the right of `=` then store it in the thing on the left of `=`."


A list of instructions that can be followed.
=<< Some Code (Finally) >>=
function repeat(x) {
    return x + repeat(x);
}
Declarative "The repeat of `x` is `x` appended to the repeat of `x`"


An infinite list of `x`!
Imperative "To evaluate the repeate of `x`, first evaluate the repeat of `x`"


...Yeah, you see the problem?
=<< Big Brain Moment >>=
Programming languages ARE human languages.
=<< Some Code (Finally) >>=

Real Haskell Code That Runs

repeat x = x ++ repeat x
threeFs = take 3 (repeat "f")

> "fff"
            
=<< Some Code (Finally) >>=

Real JavaScript Code That Hangs

function repeat(x) {
    return x + repeat(x);
}

threeFs = repeat("f").substr(0,3);

> InternalError: too much recursion
            
=<< Promises >>=
>:(
>:(
>:(
>:(
>:(
>:(
>:(
>:(
>:(
>:(
Promises do not make things asynchronous!
=<< Promises >>=

What problems do promises solve?

  • Asynchronous request management
  • Error handling for computations which may fail
  • Probably more too

What are promises then?

  • A value (which may or may not be available yet) wrapped in some additional context
  • Functions to instantiate the wrapper (Promise.resolve, Promise.reject)
  • A rule for composing these wrapped values (then)
=<< Promises >>=

Terminology/syntax recap

JavaScript arrow functions

x => x + 1;
is equivalent to:
function(x) {
    return x+1;
}

Composition

A method of combining two things of the same type in to another thing that is also of the same type
=<< Promises >>=

Promise composition

The `then` method of a promise accepts a function as an argument. This function takes a value and returns a new promise. The new promise "combines" the previous two using the "composition rule" given to `then`.
Promise.resolve("Some value").then(x => Promise.resolve(x + " and another value")).then(x => Promise.resolve(x + " and another one!"));
There are three promises in the above snippet. One, two, three.
Promise.resolve("Some value").then(x => Promise.reject("It's all gone wrong")).then(x => Promise.resolve(x + " and another one!"));
The above snippet still results in a promise, but it is a "failed" one. Any subsequent `then` after the `reject` has no effect.
=<< Promises >>=

OK great but who cares?

  • We are declaring how we want promises to compose
  • The order that the promises actually resolve their values in is not important
    • In other words, we don't have to worry about timing anymore
  • We can declare a failure anywhere in the chain
=<< Promises >>=
atemporality?
declaritive?
functions?
composition?
=<< Promises >>=

Woah!

JavaScript

Promise.resolve("We start here")
    .then(x => Promise.resolve(x + " then get here"))
    .then(x => Promise.resolve(x + " and finally here!"))

Haskell

Right("We start here")
     >>= (\x -> Right (x ++ " then get here"))
     >>= (\x -> Right (x ++ " and finally here!"))
=<< Promises >>=

Woah!

JavaScript

Promise.resolve("We start here")
    .then(x => Promise.resolve(x + " then get here"))
    .then(x => Promise.reject("Oh noes!"))
    .then(x => Promise.resolve(x + " and finally here!"))

Haskell

Right("We start here")
    >>= (\x -> Right (x ++ " then get here"))
    >>= (\x -> Left ("Oh noes!"))
    >>= (\x -> Right (x ++ " and finally here!"))