Skip to content

Instantly share code, notes, and snippets.

@jethrolarson
Last active January 27, 2017 23:19
Show Gist options
  • Save jethrolarson/546cefe7393ba6197b05d9aec7c20a88 to your computer and use it in GitHub Desktop.
Save jethrolarson/546cefe7393ba6197b05d9aec7c20a88 to your computer and use it in GitHub Desktop.
presentation on type signatures in js

Type Signatures for your JS

Y THO

A form of documentation that helps developers understand what a function does.

// toUpper :: String -> String
const toUpper = str => str.toUpperCase()
// trim :: String -> String
const trim = str =>
    str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '')

How functions fit together

    trim(toUpper(' foo   ')) // 'FOO'
// inc :: ???
const inc = x => x + 1

Type variables

Document what functions DON'T do

// identity :: a -> a
const identity = x => x
// len :: [a] -> Number
const len = arr => arr.length

currying

// max :: Number -> Number -> Number
const max = x => y =>
    x > y ? x : y
// split :: ???
const split = splitOn => str =>
    str.split(splitOn)
// always :: ???
const always = x => y => x

Other examples: join, add, match, gt

functions as arguments

// thrush :: a -> (a -> b) -> b
const thrush = arg => f => f(arg)

thrush("bob")(toUpper) // 'BOB'

// foo :: ???
const foo = thrush('frank')
// map :: (a -> b) -> [a] -> b
const map = f => arr => arr.map(f)
// pipe :: ((a -> b), (b -> c)) -> a -> c
const pipe = (firstFn, secondFn) =>
    arg => secondFn(firstFn(arg))
// String -> String
// composed with
// String -> String
// =
// trimUp :: String -> String
const trimUp = pipe(trim, toUpper)
pipe(
    len, // [a] -> Number
    inc // Number -> Number
)(['contents', 'dont', 'matter']) // 4
// filter :: ???
const filter = predicate => arr =>
    arr.filter(predicate)

What are some predicates?

What happens to filter's type when we apply a predicate?

next lesson

containers, sum types, maybe, either

Resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment