ZERO runtime errors

@alpacaaa

undefined is not a function

Functional Programming

Javascript

Null? Undefined?

Let's get some help from the compiler!

const users = {
    bob: { name: 'Bob', gender: 'male', },
    alice: { name: 'Alice', gender: 'female' },
}


function getUser(id) {
    if (users[id]) {
        return users[id]
    }

    // otherwise???

    // throw Error() ??
    // return null ??
}

getUser('tom').gender // playing with 🔥🔥🔥
// Typescript

type Option<A> = None<A> | Some<A>

function getUser(id): Option<User> {
    if (users[id]) {
        return some(users[id])
    }

    return none()
}

getUser('tom')
.getOrElse(
    (user) => user.gender,
    'other'
)

Where to go from here

  • Typescript
  • Elm
  • Purescript
  • F#
  • Ocaml
    (Bucklescript - ReasonML)

Join the revolution

#fp on slack

github.com/gcanti/functional-programming

Zero runtime errors in JS

By Marco Sampellegrini