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

Functional Programming in PHP - PHP Conference ...

Functional Programming in PHP - PHP Conference Japan 2020

Avatar for Lochemem Bruno Michael

Lochemem Bruno Michael

December 12, 2020
Tweet

More Decks by Lochemem Bruno Michael

Other Decks in Programming

Transcript

  1. Lochemem Bruno Michael • PHP enthusiast from Kampala, Uganda •

    Functional Programming aficionado • Writes PHP, JavaScript, and some C++ • Recently graduated college • Maintains PHP userland packages and extensions • Authored a book • Loves hoops, movies, and video games
  2. Functional Programming, huh? • A declarative programming paradigm • Mandates

    that pure functions be written • Saliently features the use of expressions • Emphasizes the importance of composition
  3. What are the benefits? • Functional Programming helps address complexity

    ◦ Complexity often deters project advancement ◦ Functions exist only as callable units and do not affect histories • FP techniques truncate the cognitive load of developing software ◦ Short-term memory is a finite resource ◦ FP conditions one to parameterize constructs ◦ The paradigm sturdies one’s intellection when writing programs
  4. Not so pure! • Mutable global variable (side-cause) • Value

    mutated after function is called (side-effect)
  5. Referential Transparency • Is a quality of pure functions •

    Dictates that pure functions calls should be substitutable with their outputs
  6. What we know • Classes are Composite Data Types •

    Encapsulation • Property visibility • Inheritance (is-a relationships) • Polymorphism • Setters and Getters What we may not know • Alan Kay’s biological analogy • Immutability in objects • A message-passing API
  7. Not pure! • Setter mutates internal state whenever invoked •

    Each setter call increases temporal coupling
  8. Pure Functions • Do not create side-effects • Yield the

    same output given the same input • Are referentially transparent
  9. Some FP sauce to add to your program • Partial

    application • Currying • Map, Filter, Fold/Reduce • Pattern Matching • Persistent Data Structures
  10. Currying • Decompose a function into a series of sub-functions

    • Each sub-function takes one argument
  11. Partial Application • Similar to currying • Also useful for

    deferred function calls • Each sub-function has variable arity
  12. Map, Filter, Fold • Higher-order functions for array transformations •

    Highly composable • Single iteration for every transformation
  13. Pattern Matching • Native support in pure FP languages •

    Useful whenever data has discernible patterns • Especially good for flow-control • Composable, as a function
  14. Immutable Lists • Persistent data structure • Each transformation outputs

    a new copy • Single iteration for each transformation
  15. Messy, huh? Why? • Many operations are inherently impure •

    Exceptions - used commonly - are also inherently impure • IO can get complicated
  16. Impure IO? • Filesystem interaction • Writing to and reading

    from standard output and standard input • Opening windows • Database interactions
  17. How you can cope with this • Use functors and

    monads • Parameterize (might lead to callback hell) • Favor composition with forward function chaining • Use persistent data structures
  18. Functor • Something you can map over • Typically a

    type class • A type of category • Supports forward chaining
  19. Monad • A special type of functor • Convenient because

    of inherent ad-hoc value • Especially good for flow-control
  20. Real world monads • Either and Maybe union types •

    IO monad • State monad • Reader monad • Writer monad
  21. Maybe Monad • Composed of Just and Nothing types •

    Can evaluate to one of either sub-type at runtime • Useful for error handling
  22. Either Monad • Composed of Left and Right types •

    Allows for more succinct error handling • Great for the Railway pattern
  23. Reader Monad • Useful for sharing data amongst functions •

    Akin to Dependency Injection (DI) in OO
  24. Writer Monad • Great for logging data • Can, in

    some contexts, be used for event sourcing
  25. How do I know I’m doing it right? The functions

    you write should have the following qualities: • Purity • Referential Transparency • Composability • Conciseness
  26. The future looks bright for FP in PHP! • New

    language features • New RFCs • New libraries • More explanatory material • All-around language consistency