Functional Programming

Updated: 2019-01-07

Side Effects

to yield actions(e.g. change states) in addition to return values. (i.e. returning values is "main", everything else is "side")

Pure Functions

  • no side-effects
  • operate on immutable data, do not alter the original input data, but make a copy and transform it from there.

Benefits:

  • composable, self-contained and stateless
  • code is easier to refactor
  • no writes, so no synchronization, no locks, semaphores, mutexes
  • result only depend on arguments, easy to test, debug, understand, and reuse
  • modeled after mathematics.
  • can chain transformations of an immutable collection to its desired result.
  • enable lazy evaluation(since it does not depend on states, only inputs)

Functions are First-Class

Functional programming requires that functions are first-class

  • functions are treated like any other values; can be passed as arguments to other functions; can be the returned result of a function.
  • possible to define and manipulate functions from within other functions.

Higher-order functions (HOFs)

Higher-order functions: functions that take other functions as their arguments. e.g. map

  • often used to implement domain-specific languages
  • can be usually simulated in object-oriented languages by functions that take function-objects, also called functors