EMZETT.
Login

Functional programming

In short: Functional programming builds programs from functions that compute a result only from their inputs and change nothing else. That makes code easier to test, understand and run in parallel.

Core ideas

  1. Pure functions: same input always gives the same output, no side effects (see Pure function).
  2. Immutable data: instead of changing a list you create a new one (Immutability).
  3. Functions as values: you can pass, return and store them (higher-order function, lambda expression).
  4. Replace loops with transformation: map, filter, reduce.

Example (Python)

numbers = [1, 2, 3, 4, 5, 6]
even_squares = [x * x for x in numbers if x % 2 == 0]   # [4, 16, 36]
 
from functools import reduce
total = reduce(lambda a, b: a + b, even_squares, 0)       # 56

The same with map/filter: sum(map(lambda x: x*x, filter(lambda x: x%2==0, numbers))).

More terms

  • Closure (closure) and currying (Currying): functions that “capture” values or receive arguments step by step.
  • Recursion instead of loops (recursion).
  • Lazy evaluation (Lazy Evaluation): compute only when the result is needed.
  • Monads (e.g. Maybe/Option, Result): patterns to handle errors and missing values without exceptions.

In practice

Purely functional languages are Haskell, Elm or Clojure. But almost every modern language (JavaScript, Python, Java, Kotlin, Rust, C#) offers functional tools. You mix: pure functions at the core, side effects at the edges (database, screen, network). This helps enormously with concurrency because there is no shared mutable data (concurrency).

See also: Object-oriented programming