Skip to main content

Functional Programming (FP)

In functional programming (FP), while there isn't a group as famous as the "Gang of Four" for object-oriented programming (OOP), there are several well-known design patterns and concepts that are widely used. Unlike OOP, where design patterns often revolve around class and object structures, functional programming patterns are more about how functions are used and composed.

Each of these patterns addresses a particular aspect of functional programming and helps in structuring code in a way that leverages the strengths of the FP paradigm. They are widely used in functional languages like Haskell, Scala, and also in multi-paradigm languages like JavaScript (especially with libraries like Ramda or Lodash FP).

Some key functional programming patterns include:

Monads​

A design pattern used to describe computations as a series of steps. Common examples include the Maybe monad, which handles null checking, and the IO monad, which deals with side effects.

Functors​

They're about applying a function over a wrapped value. In JavaScript, arrays are a common example of functors, where you can apply a function to each element with map.

Currying​

This is the process of transforming a function with multiple arguments into a sequence of functions each taking a single argument. It’s useful for creating partially applied functions.

Function Composition​

Creating new functions by composing existing ones. It’s a core concept in FP, allowing you to build complex operations out of simpler functions.

Immutability​

While not a pattern per se, the practice of immutability (unchanging data) is central to functional programming, leading to more predictable and maintainable code.

Pure Functions​

Functions that have no side effects and always produce the same output for the same input. They are easier to test, reason about, and parallelize.

Recursion​

Many functional programming languages favor recursive functions over iterative loops for repeating operations.

Higher-Order Functions​

Functions that take other functions as arguments or return them as results. Examples include map, filter, and reduce.

Lenses​

A pattern for managing state in a functional way, particularly for dealing with nested structures. Lenses provide a functional interface for getting and setting data within a complex object.

Algebraic Data Types (ADTs)​

These include things like unions and product types, which allow for more expressive and safer type definitions.