Skip to content

Functional Programming

  • Side Effect: An unpredictable change in the state of the environment that would break the substitution model and function will no longer be referentially transparent.
  • When a side-effect is tracked and controlled we call it an effect (Scalar Conf 2025)
    • functions returning effectful results are hard to compose
  • Effects types:
    • Monadic: They use monads (flatMap and map) to compose functions using. e.g Cats-Effect and ZIO
    • Algebraic: Function type tells exactly what effects are being used, and can handle each effect separately

Category theory

  • A category S consists of
    • objects A, B, C etc.
    • morphisms (or arrows) that map between different objects and have two fundamental properties
      • composition: morphisms h . (g . f) == (h . g) . f
      • identity: f . id == f Category Theory
  • In Scala, objects correspond to Types and morphisms correspond to pure functions, associative composition is similar to andThen
  • A Functor is a structure preserving mapping between categories
    • In Scala, since we always work with types and functions, functors are always in the same category, ie, endofunctors
  • Tagless Final encoding uses functions whereas Initial encoding uses data types
    • Initial: hard to extend algebra (add operations), Final: hard to add interpreters