A (very brief) into to Functional Programming
-
Upload
brooklyn-zelenka -
Category
Technology
-
view
151 -
download
0
Transcript of A (very brief) into to Functional Programming
INTRODUCTION TO FUNCTIONAL
PROGRAMMING
A very brief
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
• Recursion
• Stateless / explicit state
• Higher-order functions
• Data types
• Type safety
• 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
• Lazy evaluation
• Composition
• Referential transparency
• Currying & partial function application
• Reusability
• Pattern matching
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
FunctionalObjective
Obviously opposites
RELATIONSHIP WITH OBJECT-ORIENTATIONO
bjec
tive-
ness
Functional-ness
Ruby
JavaScript
Haskell
OCaml
Erlang
Swift
CCommon LISP
Assembly
Clojure
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
# Ruby Fold Example: sum numbers range = (0..99_999_999)
## Imperative/procedural total = 0 i = 0 while i < range.count do total += range[i] i += 1
end total
## Functional range.reduce(:+)
• Don’t need to calculate the length of the array• Don’t rely on a throwaway variable
## Roughly def reduce_add(array) return 0 if array == [] array[0] + reduce_add(array[1..-1]) end
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)