Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Demystify Functional Programming in Swift

Ennio
February 28, 2018

Demystify Functional Programming in Swift

Trying to explain Functional Programming core concepts stripping away category theory (but with a lot of respect!) and apply them to Swift.

Ennio

February 28, 2018
Tweet

More Decks by Ennio

Other Decks in Programming

Transcript

  1. DEMYSTIFY FUNCTIONAL PROGRAMMING IN (OR TRYING TO … ) ENNIO

    MASI SENIOR IOS DEV @ YOOX NET-A-PORTER
  2. IN COMPUTER SCIENCE, FP IS A PROGRAMMING PARADIGM - A

    STYLE OF BUILDING THE STRUCTURE AND ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS CHANGING - STATE AND MUTABLE DATA. IN COMPUTER SCIENCE, FP IS A PROGRAMMING PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS CHANGING - STATE AND MUTABLE DATA. FUNCTIONAL PROGRAMMING IN COMPUTER SCIENCE, FP IS A PROGRAMMING PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS CHANGING - STATE AND MUTABLE DATA. IN COMPUTER SCIENCE, FP IS A PROGRAMMING PARADIGM - A STYLE OF BUILDING THE STRUCTURE AND ELEMENTS OF COMPUTER PROGRAMS - THAT TREATS COMPUTATION AS THE EVALUATION OF MATHEMATICAL FUNCTIONS AND AVOIDS CHANGING - STATE AND MUTABLE DATA. Someone on Wikipedia
  3. FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM

    DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION APPLICATION, AND RECURSION. MANY FUNCTIONAL PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS ON THE LAMBDA CALCULUS. Someone else on Wikipedia FUNCTIONAL PROGRAMMING FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION APPLICATION, AND RECURSION. MANY FUNCTIONAL PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS ON THE LAMBDA CALCULUS. FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION APPLICATION, AND RECURSION. MANY FUNCTIONAL PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS ON THE LAMBDA CALCULUS. FP HAS ITS ORIGINS IN LAMBDA CALCULUS, A FORMAL SYSTEM DEVELOPED IN THE 1930S TO INVESTIGATE COMPUTABILITY, THE ENTSCHEIDUNGSPROBLEM, FUNCTION DEFINITION, FUNCTION APPLICATION, AND RECURSION. MANY FUNCTIONAL PROGRAMMING LANGUAGES CAN BE VIEWED AS ELABORATIONS ON THE LAMBDA CALCULUS.
  4. FOUNDATIONS FP IS AN APPROACH BASED ON FUNCTION CALLS OOP

    FP Single Responsability functions Open Closed Principle functions Dependecy Inversion functions Factory pattern functions … …
  5. FOUNDATIONS F(X) = Y ▸ Write predictable functions FP IS

    AN APPROACH BASED ON FUNCTION CALLS ▸ Code easier to test ▸ Thread-safe codebase ▸ Functions chaining
  6. FOUNDATIONS FP IS AN APPROACH BASED ON FUNCTION CALLS FP

    IS AN APPROACH BASED ON PURE FUNCTION CALLS
  7. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON

    PURE FUNCTION CALLS currentDate 20/11/2018 INPUT tellMeTheTruth(..) ? OUTPUT
  8. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON

    PURE FUNCTION CALLS currentDate 20/11/2018 INPUT tellMeTheTruth(..) false OUTPUT current date 21/11/2018
  9. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON

    PURE FUNCTION CALLS currentDate 20/11/2018 INPUT migrationStatus() true OUTPUT current date 19/11/2018
  10. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON

    PURE FUNCTION CALLS NOT PURE NO SIDE EFFECTS, TESTABLE, THREAD SAFE PURE
  11. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON

    PURE FUNCTION CALLS NOT PURE NO SIDE EFFECTS, TESTABLE, THREAD SAFE PURE
  12. FOUNDATIONS F(X) = Y FP IS AN APPROACH BASED ON

    PURE FUNCTION CALLS NOT PURE NO SIDE EFFECTS, TESTABLE, THREAD SAFE PURE
  13. FOUNDATIONS FP IS AN APPROACH BASED ON FUNCTION CALLS FP

    IS AN APPROACH BASED ON PURE FUNCTION CALLS FP IS AN APPROACH BASED ON COMPOSITION OF PURE FUNCTION CALLS
  14. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE

    FUNCTION CALLS f(2) —> 2 * 3 = 6 g(6) —> 6 * 6 = 36 G(F(A)) = B g(f(2)) ➡ f(2) = 6 ➡ g(6) = 36
  15. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE

    FUNCTION CALLS FUNCTIONS ARE FIRST CLASS CITIZENS HIGH ORDER FUNCTIONS SWIFT SUPPORTS FP
  16. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE

    FUNCTION CALLS COMPOSITION F G A B C A C F G ⏭
  17. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE

    FUNCTION CALLS F: A —>B G: B —>C F: INT —>INT G: INT —>INT F: INT —>INT G: INT —>INT H: INT —>STR
  18. FOUNDATIONS FP IS AN APPROACH BASED ON COMPOSITION OF PURE

    FUNCTION CALLS ▸ Write predictable functions ▸ Code easier to test ▸ Thread-safe codebase ▸ Features chaining
  19. FOUNDATIONS: CURRYING CURRYING IS A WAY TO BREAKDOWN FUNCTIONS COMPLEXITY

    $ ▸ A curried function is executed only when all the parameters are set F(X, Y) —> Z ▸ Think to a function with N parameters as a function with N times 1 parameter F(X)(Y) —> Z
  20. FOUNDATIONS: IMMUTABILITY OOP (IMPERATIVE) VS FP TAIL RECURSION IS NOT

    OPTIMISED IN SWIFT TAIL RECURSION SWIFT PARTIALLY SUPPORTS FP
  21. “A monad is just a monoid in the category of

    endofunctors” Someone on Internet FOUNDATIONS: FUNCTORS & MONADS
  22. FOUNDATIONS: FUNCTORS & MONADS FUNCTORS ▸ SINCE OPTIONAL IS A

    “CONTEXT”, WE NEED A WAY TO EXTRACT THE POSSIBLE VALUE FROM IT ▸ MAP
  23. FOUNDATIONS: FUNCTORS & MONADS FUNCTORS ▸ SINCE OPTIONAL IS A

    “CONTEXT”, WE NEED A WAY TO EXTRACT THE POSSIBLE VALUE FROM IT ▸ MAP [<$>, <!>] OPTIONAL MAP VALUE OR NONE
  24. FOUNDATIONS: FUNCTORS & MONADS FUNCTORS OPTIONAL MAP VALUE OR NONE

    A FUNCTOR IS A TYPE THAT CONTAINS A VALUE AND PROVIDES A MAP FUNCTION AS INTERFACE FUNCTOR MAP VALUE OR NONE HINT: OPTIONAL IS A FUNCTOR FP
  25. FOUNDATIONS: FUNCTORS & MONADS FUNCTORS A FUNCTOR IS A TYPE

    THAT CONTAINS A VALUE AND PROVIDES A MAP FUNCTION AS INTERFACE HINT: OPTIONAL IS A FUNCTOR ▸ Identity Law ▸ Composition Law
  26. FOUNDATIONS: FUNCTORS & MONADS MONADS A MONAD IS JUST A

    MONOID IN THE BLA BLA BLA … ▸ Well a monad is just a type on which we can apply flatMap (bind) FUNCTIONS CHAINING (ON VALUES) HINT: OPTIONAL IS A MONAD $ ▸ flatMap [>>=]
  27. FOUNDATIONS: FEATURES CHAINING MONADS IN PRACTICE Well a monad is

    just a type on which we can apply flatMap (bind)
  28. FOUNDATIONS: FEATURES CHAINING string f1 f2 f Out Monad String

    bool bool bool error error error error bind return return
  29. FOUNDATIONS: FEATURES CHAINING string f1 f2 f Out Monad bool

    bool bool bool error error error error bind return return FEATURES CHAINING (ON VALUES) ERROR MANAGEMENT VS