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

Functional Programming in PHP

Functional Programming in PHP

Lochemem Bruno Michael

August 06, 2020
Tweet

More Decks by Lochemem Bruno Michael

Other Decks in Programming

Transcript

  1. Functional Programming, huh? • A declarative programming paradigm • Mandates

    that pure functions be written • Saliently features the use of expressions • Emphasizes the importance of composition
  2. 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
  3. Not so pure! • Mutable global variable (side-cause) • Value

    mutated after function is called (side-effect)
  4. Also not pure! • Setter mutates internal state whenever invoked

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

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

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

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

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

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

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

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

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

    from standard output and standard input • Opening windows • Database interactions
  14. 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
  15. Functor • Something you can map over • Typically a

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

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

    IO monad • State monad • Reader monad • Writer monad
  18. Either and Maybe Types • Great for error handling •

    Emphasize writing two-track functions (Railway pattern)
  19. State Monad • Stores an initial state • Transforms copies

    of initial state in separate container
  20. Reader Monad • Useful for sharing data amongst functions •

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

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

    you write should have the following qualities: • Purity • Referential Transparency • Composability • Conciseness