A (very brief) into to Functional Programming

A presentation at Lunch & Learn in November 2014 in Vancouver, BC, Canada by Brooklyn Zelenka

Slide 1

Slide 1

A very brief INTRODUCTION TO FUNCTIONAL PROGRAMMING

Slide 2

Slide 2

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

Slide 3

Slide 3

WHAT IS FUNCTIONAL PROGRAMMING • Family of languages • Not one monolithic thing • Collection of attributes & (very) general style

Slide 4

Slide 4

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)

Slide 5

Slide 5

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

Slide 6

Slide 6

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”

Slide 7

Slide 7

RELATIONSHIP WITH OBJECT-ORIENTATION Obviously opposites Objective Functional

Slide 8

Slide 8

RELATIONSHIP WITH OBJECT-ORIENTATION Objective-ness Ruby Swift OCaml JavaScript Clojure Erlang C Assembly Common LISP Haskell Functional-ness

Slide 9

Slide 9

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

Slide 10

Slide 10

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!

Slide 11

Slide 11

MAPPING, FILTERS, FOLDING & ACCUMULATION • Much simpler to reason about & cleaner syntax • Often has a performance advantage

Ruby Fold Example: sum numbers range = (0..99_999_999) ## Imperative/procedural total = 0 i = 0 while i < range.count do total += range[i] • Don’t need to calculate the length of the array i += 1 • Don’t rely on a throwaway variable end total ## Roughly def reduce_add(array) ## Functional return 0 if array == [] range.reduce(:+) array[0] + reduce_add(array[1..-1]) end

Slide 12

Slide 12

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

Slide 13

Slide 13

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)

Slide 14

Slide 14

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)