A (very brief) into to Functional Programming

14
INTRODUCTION TO FUNCTIONAL PROGRAMMING A very brief

Transcript of A (very brief) into to Functional Programming

Page 1: A (very brief) into to Functional Programming

INTRODUCTION TO FUNCTIONAL

PROGRAMMING

A very brief

Page 2: A (very brief) into 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

Page 3: A (very brief) into to Functional Programming

WHAT IS FUNCTIONAL PROGRAMMING

• Family of languages

• Not one monolithic thing

• Collection of attributes & (very) general style

Page 4: A (very brief) into to Functional Programming

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)

Page 5: A (very brief) into to Functional Programming

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

Page 6: A (very brief) into to Functional Programming

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”

Page 7: A (very brief) into to Functional Programming

RELATIONSHIP WITH OBJECT-ORIENTATION

FunctionalObjective

Obviously opposites

Page 8: A (very brief) into to Functional Programming

RELATIONSHIP WITH OBJECT-ORIENTATIONO

bjec

tive-

ness

Functional-ness

Ruby

JavaScript

Haskell

OCaml

Erlang

Swift

CCommon LISP

Assembly

Clojure

Page 9: A (very brief) into to Functional Programming

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

Page 10: A (very brief) into to Functional Programming

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!

Page 11: A (very brief) into to Functional Programming

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

Page 12: A (very brief) into to Functional Programming

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

Page 13: A (very brief) into to Functional Programming

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)

Page 14: A (very brief) into to Functional Programming

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)