A very brief INTRODUCTION TO FUNCTIONAL PROGRAMMING
A presentation at Lunch & Learn in November 2014 in Vancouver, BC, Canada by Brooklyn Zelenka
A very brief INTRODUCTION TO FUNCTIONAL PROGRAMMING
WHY FP? • More structured (like goto vs OO) • Clear & concise • Performance, parallelism, concurrency • Reusable, generics • Expand your horizons! • Become a more flexible programmer • Have fun
WHAT IS FUNCTIONAL PROGRAMMING • Family of languages • Not one monolithic thing • Collection of attributes & (very) general style
TWO SOLUTIONS TO THE SAME ENTSCHEIDUNGSPROBLEM Alan Turing Alonzo Church Imperative (Instructions) Functional (everything is a function) Global State (“memory”) Stateless (like REST) Mechanistic (Turing Machine) Mathematical (Lambda Calculus)
CONSTELLATION OF ATTRIBUTES • Immutability • Lazy evaluation • Recursion • Composition • Stateless / explicit state • Referential transparency • Higher-order functions • Currying & partial function application • Data types • Reusability • Type safety • Pattern matching • Don’t need all of these! • Some languages enforce these more than others • Can do some FP in traditionally “imperative” languages, too • Java, Ruby, JavaScript, and so on
APPROACH • Imperative style describes how to do things • Functional style is often declarative • describes what things are • …plus data and transformations between them • Sometimes called “data oriented”
RELATIONSHIP WITH OBJECT-ORIENTATION Obviously opposites Objective Functional
RELATIONSHIP WITH OBJECT-ORIENTATION Objective-ness Ruby Swift OCaml JavaScript Clojure Erlang C Assembly Common LISP Haskell Functional-ness
IMMUTABILITY, PURITY & RECURSION • • Advantages • Get parallelization “for free” because no shared state • No deadlocks or strange state infection bugs • Compact and/or “does the work for you” Disadvantages • Recursion can be mind bending at first • Recursion may have time or memory space costs
IMMUTABILITY MAKES SENSE What you type What the compiler says x = 42 Okay, “x” means 42 x Hmmm… 42! x = 99 What are you? Some kind of liar? “x” means 42, obviously! x Hmmm… still 42!
MAPPING, FILTERS, FOLDING & ACCUMULATION • Much simpler to reason about & cleaner syntax • Often has a performance advantage
LIST COMPREHENSIONS & CONSTRAINT PROGRAMMING — Haskell List Comprehension [(x, y) | x <- [50..100], y <- [1..99], x mod
7 == 3, x mod
y == 42] — RESULT: [(87,45),(94,52)]
— Haskell List Comprehension: INFINITE EDITION [(x, y) | x <- [50..], y <- [1..99], x mod
7 == 3, x mod
y == 42] — RESULT: [(87,45),(94,52),(101,59),(108,66),(115,73),(122,80),(129,87), (136,47),(136,94),(150,54),(164,61),(171,43),(178,68),(192,50),(192,75), (206,82),(213,57),(220,89),(234,48),(234,64),(234,96),(255,71),(262,44), (262,55),(276,78),(290,62),(297,51),(297,85),(318,46),(318,69)… …and so on, FOREVER, and while working with the data stream
FURTHER, CRAZIER STUFF • Faster custom server • Serves ~2 million requests/second • Triggered a rare Linux flaw due to high throughput • Quantum computing • Massive truly parallel systems • Parallelism “for free” (more or less)
FURTHER READING • There is so much more! We’ve barely scratched the surface • Vancouver Functional Programmers • Learn You a Haskell for Great Good • Clojure for the Brave and True • What I Wish I Knew Before Learning Haskell • Refactoring Ruby with Monads • The Underscore.js Source • Don’t Fear the Monad • Don’t be Afraid of Functional Programming (SmashingMag)