Why Functional Programming?
@volpegabriel87
@gvolpe
Paradox of programming
@volpegabriel87
- Local perspective
- Progress oriented
- Detail
- Vast memory
- Pretty reliable
- Machine language
Machine / Human impedance mismatch
@gvolpe
"A crash course in Category Theory" by Bartosz Milewski (2017)
- Global perspective
- Goal oriented
- Idea
- Limited memory
- Error prone
- Mathematics
λ-calculus
@volpegabriel87
@gvolpe
"A set of postulates for the foundation of logic" by Alonzo Church (1932)
A universal model of computation that can be used to simulate any Turing Machine
A formal system for expressing a computation based on function abstraction and application
LISP
@volpegabriel87
@gvolpe
"LISP specification" by John McCarthy (1958)
Originally created as a practical mathematical notation for computer programs, influenced by the Lambda Calculus system
He also invented garbage collection (implemented in LISP)
Erlang
@volpegabriel87
@gvolpe
Developed by Ericsson ( Joe Armstrong, Robert Virding, and Mike Williams) in 1986
Distributed, fault-tolerant and highly available
Open Telecom Platform (OTP)
@volpegabriel87
@gvolpe
Haskell committee at FPCA (1987)
Haskell 1.0 (1990)
First versions didn't support I/O
"Escape from the ivory tower: the Haskell journey" by Simon P. Jones
Functional Programming
Functional Programming
@volpegabriel87
@gvolpe
"Why functional programming matters" by John Hughes (1990)
- Higher-order functions
- Lazy evaluation
Leads to better modularity
Functional Programming
@volpegabriel87
@gvolpe
Functional Programming
@volpegabriel87
@gvolpe
Monads
foo :: Maybe Int
foo = Just 1
bar :: Maybe Int
bar = Just 2
sum :: Maybe Int
sum = foo >>= \x -> bar >>= \y -> return (x + y)
sum' :: Maybe Int
sum' = do
x <- foo
y <- bar
return (x + y)
Functional Programming
@volpegabriel87
@gvolpe
"Imperative functional programming" by Simon P. Jones and Philip Wadler (1992)
- IO Monad
- Connecting I/O operations
Ability to mix imperative and purely functional programming, without ruining either
Functional Programming
@volpegabriel87
@gvolpe
IO Monad
toUpper :: Char -> Char
getChar :: IO Char
putChar :: Char -> IO ()
(>>=) :: IO a -> (a -> IO b) -> IO b
return :: a -> IO a
getChar >>= (\a -> putChar a)
@volpegabriel87
@gvolpe
Haskell & PureScript & Scala
Console program
main :: IO ()
main = do
putStrLn "Enter your name:"
n <- readLn
putStrLn ("Welcome " <> n)
val program: IO[Unit] =
for {
_ <- IO(println("Enter your name:"))
n <- IO(StdIn.readLine)
_ <- IO(println(s"Welcome $n"))
} yield ()
main :: Effect Unit
main = log "Hello, World!"
Functional Programming
@volpegabriel87
@gvolpe
- Haskell
- PureScript
- Scala
- ELM
- OCaml
- Nix
DevOps industry is choosing Nix for its reproducible builds and immutability
Languages
Functional Programming
@volpegabriel87
@gvolpe
Nixpkgs
$ curl https://nixos.org/nix/install | sh
$ nix-env -i hello
$ which hello
/home/user/.nix-profile/bin/hello
$ hello
Hello, world!
$ nix-env -e hello
$ nix-shell -p hello
Functional Programming
@volpegabriel87
@gvolpe
FP languages can be reasoned about mathematically
Functional Programming
@volpegabriel87
@gvolpe
- Facebook (spam filter in Haskell)
- Twitter (built on Scala)
- Google (IT infrastructure in Haskell)
- Microsoft (serialization system in Haskell)
- New York Times (image processing in Haskell)
Industry adoption
@volpegabriel87
@gvolpe
@volpegabriel87
@gvolpe
<<SCALA MVD>>
Gracias totales!
@volpegabriel87
@gvolpe
why-fp
By Gabriel Volpe
why-fp
Why Functional Programming?
- 2,698