Why functional

Hipsters

Pure functions

var name = "Magnus" function greet() { console.log("Hi, I am " + name) } greet() function greet(name) { return "Hi " + name } greet("Codenight")

Don't iterate with mutable state... Transform

Didacool @ wikimedia
function greet(name) { return "Hi " + name } function addExclamation (str, exclamation) { return str.trimRight() + exclamation } const names = ["magnus", "codenight"] var greetings = [] for(var i = 0; i < names.length; i++) { greetings[i] = addExclamation(greet(names[i]), "!") } greetings

Higher order functions

jamesjordan @ flickr
              
                const greet = name => "Hi " + name

                const greetAndAddExclamation = (greetfn, name) => 
                  exclamation => greetfn(name).trimRight() + exclamation

                function greetAndAddExclamation (greetfn, name) {
                  return function (exclamation) { 
                    greetfn(name).trimRight() + exclamation 
                  }
                }

                greetAndAddExclamation(greet, "magnus ")("!")
              
            
const greet = name => "Hi " + name const greetAndAddExclamation = (greetfn, name) => exclamation => greetfn(name).trimRight() + exclamation function greetAndAddExclamation2 (greetfn, name) { return function (exclamation) { greetfn(name).trimRight() + exclamation } } greetAndAddExclamation(greet, "magnus ")("!")

transform with map, reduce, filter

              
                const greet = name => "Hi " + name
                const names = ["magnus", "codenight"]

                const addExclamation = 
                  (str, exclamation) => str.trimRight() + exclamation

                names.map(greet)
                     .map(x => addExclamation(x, "!"))
              
            
const greet = name => "Hi " + name const names = ["magnus", "codenight"] const addExclamation = (str, exclamation) => str.trimRight() + exclamation names.map(greet).map(x => addExclamation(x, "!"))

Function composition

                
                  const greet = name => "Hi " + name

                  const addExclamation = exclamation => 
                    str => str.trimRight() + exclamation

                  const capitalize = name => 
                    name[0].toUpperCase() + name.substr(1)

                  addExclamation("!")(greet(capitalize("magnus")))
                
            
//const compose = (...fns) => fns.reduceRight((f, g) => (...args) => f(g(...args))) const capitalize = name => name[0].toUpperCase() + name.substr(1) const greet = name => "Hi " + name const addExclamation = exclamation => str => str.trimRight() + exclamation //const addGreetingAndExclamation = compose(greet, addExclamation("!")) addExclamation("!")(greet(capitalize("magnus")))

Mutable state is the new spaghetti code

Spaghetti code
oskay @ flickr

Concurrency

                
                  const validLength = name => name.length <= 6

                  const onlyValidChars = 
                    name => ['!', '?'].every(c => !name.includes(c))

                  const applyAllPar = ( ... fns ) => 
                    arg => fns.par.map(f => f(arg))

                  const validations = applyAllPar(validateLength, validateChars)

                  const names = new Parallel(["codenight", "magnus"])

                  names.filter(validations.every(Boolean))
                       .map(addGreetingAndExclamation)