Functional Programming


Contents

  1. Introduction
  2. Main Concepts
    1. First Class Functions
    2. Higher Order Functions (HOC)
    3. Pure Functions
    4. Recursion
    5. Types
  3. Sources

Introduction

Functional programming is a programming paradigm in which a program is written using Functions (also called subroutines) as it’s primary means of expressing the desired behavior of the program.

Functional programming is highly related to the concept of a Function in Mathematics, and traces it’s origin to Lambda Calculus, as introduced by Alonzo Church, and further developed by Alan Turing in the 20th Century.

Purely functional programming, is a subset of the functional programming paradigm in which each function acts as a deterministic mathematical function and should produce an identical result when identical arguments are passed in, meaning that it cannot be affected by side effects or mutable state.

As a programming paradigm, Functional programming exists in numerous languages; some which are considered functional languages, use functions as a key feature of the language, and languages which support functional programming alongside other programming paradigms via added added features in subsequent releases.

In this article I use Typescript as the example language.


Main Concepts

Functional Programming contains a few concepts that are not present in imperative programming or object-oriented programming. Despite this, functional programming can often be adapted for use in settings and languages that primarily cater to imperative and object-oriented use-cases.

The main concepts of integral to functional programming are:

  • First Class Functions
  • Higher Order Functions
  • Pure Functions
  • Recursion
  • Types

First Class functions

First Class functions is a feature of languages supporting functional programming in which a function:

  1. Can be passed as an argument to another functions.
  2. Can be assigned to a variable or stored in a datastructure.
  3. (Optionally) can be written without being assigned a name.

1. Passing an argument to another function.

Because Functions are First Class, meaning they can be used in the same ways as other types, they can also be passed as arguments to other functions.

In the example below, two functions definitions are passed as arguments to another function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function addition(a: number, b: number): number {
  return a + b;
}

function subtraction(a: number, b: number): number {
  return a - b;
}

type MathOperation = (a: number, b: number): number;

function addAndSubtract(a: number, b: number, add: MathOperation, sub: MathOperation) {
  return add(a, b) * sub(a, b)
}

addAndSubtract(1, 2, addition, subtraction)

2. Assignment of functions

Functions can be assigned to variables:

1
2
3
const addition = (a: number, b: number): number => {
  return a + b;
}

Functions can also be assigned as values in data structures:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const basicMathOperations = {
  addition(a: number, b: number): number => {
    return a + b;
  },
  subtraction(a: number, b: number): number => {
    return a - b;
  }
};

basicMathOperations.addition(1, 2);
// result: 3

basicMathOperations.subtraction(2, 1);
// result: 1

Higher Order Functions

A Higher Order Function is a function that can take a function as an argument.

Example:

1
2
3
4
const numberList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

const result = numberList.map((i: number) => i + 1);
// result: [1, 2, 3, 4, 5, 7, 8, 9, 10]

Functional Programming in ECMAScript

ECMAScript

Standard function

Anonymous function

Lambda function

Functional Programming with data structures

array.map

array.filter


Sources

Wikipedia