# Outline

Vague plan and goals of this talk:

• Look at FP history and motivation
• Describe FP features
• Look at where we are today

Just like OOP.

# OOP

Computational things are object-like things.

Made precise by a set of features, commonly:

• Encapsulation
• Inheritance
• Subtyping
• Late binding
• Dynamic dispatch

Ruby / Java / C# / Python / Smalltalk are all “OOP”, but vary wildly.

# FP

Programs are compositions of functions.

Concepts commonly associated with FP:

• First-class functions
• Declarative programming
• Purity
• Strong and static typing
• Being slow
• Being impractical

# History

(Based on this graph.)

Escaping from Von Neumann

Escaping from Von Neumann

## John Backus (1977)

• FP language
• Hints of declarative style
• Concentrate on composing programs

# λ-calculus (1930s)

## (λx.M)N ⇒ M[x = N]

``````def f(x):
[...]
return M

>>> f(N)
M``````

# FP concepts

## First-class functions

``````def flip_args(f):
def flipped(x, y):
return f(y, x)

return flipped``````
``flip_args = lambda f: lambda x: lambda y: f(y, x)``
``````flip :: (a -> b -> c) -> (b -> a -> c)
flip f = \x -> \y -> f y x``````

## Purity

``int getUserId(User u)``
``````int getUserId(User u) {
int id = u.id;
launchNuclearWeapons();
return id;
}``````

## Purity

``````int getUserId(User u) {
fetchIdFromDb(u);
}``````
``f :: a -> b``
``getUserId :: User -> Int``
``getUserId :: User -> IO Int``

## Compositionality

First-class functions + Purity = Compositionality

``````compose :: (b -> c) -> (a -> b) -> a -> c
compose f g = \x -> f (g x)``````

Unix pipes are compositional

``cat | grep | sort``

## Declarative programming

``````int i;
int arraySize = figureItOut(arr);
SomeData newarr[arraySize];
for (i = 0; i < arraySize; i++)
{
newarr[i] = f(arr[i]);
}
maybeDestroyOldArr(arr);``````

vs.

``newlst = map f lst``

## The Today

Some newish languages:

• 2003: Scala
• 2009: Go
• 2012: Rust
• 2014: (Apple) Swift

• All have extensive FP features

Each successive language incorporates, with a little cleaning up, all the features of its predecessors plus a few more. — John Backus, 1977

## FP for everyone: a guide

• “I hate static typing” => LISPs (Racket, Clojure), Erlang… Ruby?
• “I need my objects” => F#, Scala, Swift
• “I love Ruby” => Elixir
• “I’m a lazy person” => Haskell
• “I want to do front-end stuff” => Elm, Fay, GHCJS, Swift
• “I want to do systems programming” => Ocaml, Rust
• “I want to live in a JVM” => Clojure, Scala
• “I love fancy types” => Ocaml, F#, Haskell, Idris, Agda
• “I want to program rockets” => Coq

## Conclusion

``first10 = take 10 \$ getTwitterFollowers``