Functional Programming Jargon

Build Status

Functional programming (FP) provides many advantages, and its popularity has been increasing as a result. However, each programming paradigm comes with its own unique jargon and FP is no exception. By providing a glossary, we hope to make learning FP easier.

This is a fork of Functional Programming Jargon.

This document is WIP and pull requests are welcome!

Table of Contents

Side effects

A function or expression is said to have a side effect if apart from returning a value, it interacts with (reads from or writes to) external mutable state:

>>> print('This is a side effect!')
This is a side effect!
>>>

Or:

>>> numbers = []
>>> numbers.append(1)  # mutates the `numbers` array
>>>

Purity

A function is pure if the return value is only determined by its input values, and does not produce any side effects.

This function is pure:

>>> def add(first: int, second: int) -> int:
...    return first + second
>>>

As opposed to each of the following:

>>> def add_and_log(first: int, second: int) -> int:
...    print('Sum is:', first + second)  # print is a side effect
...    return first + second
>>>

Idempotent

A function is idempotent if reapplying it to its result does not produce a different result:

>>> assert sorted([2, 1]) == [1, 2]
>>> assert sorted(sorted([2, 1])) == [1, 2]
>>> assert sorted(sorted(sorted([2, 1]))) == [1, 2]
>>>

Or:

>>> assert abs(abs(abs(-1))) == abs(-1)
>>>

Arity

The number of arguments a function takes. From words like unary, binary, ternary, etc. This word has the distinction of being composed of two suffixes, "-ary" and "-ity". Addition, for example, takes two arguments, and so it is defined as a binary function or a function with an arity of two. Such a function may sometimes be called "dyadic" by people who prefer Greek roots to Latin. Likewise, a function that takes a variable number of arguments is called "variadic," whereas a binary function must be given two and only two arguments, currying and partial application notwithstanding.

We can use the inspect module to know the arity of a function, see the example below:

>>> from inspect import signature

>>> def multiply(number_one: int, number_two: int) -> int:  # arity 2
...     return number_one * number_two

>>> assert len(signature(multiply).parameters) == 2
>>>

Arity Distinctions

Minimum Arity and Maximum Arity

The minimum arity is the smallest number of arguments the function expects to work, the maximum arity is the largest number of arguments function can take. Generally, these numbers are different when our function has default parameter values.

>>> from inspect import getfullargspec
>>> from typing import Any

>>> def example(a: Any, b: Any, c: Any = None) -> None:  # mim arity: 2 | max arity: 3
...     pass

>>> example_args_spec = getfullargspec(example)
>>> max_arity = len(example_args_spec.args)
>>> min_arity = max_arity - len(example_args_spec.defaults)

>>> assert max_arity == 3
>>> assert min_arity == 2
>>>

Fixed Arity and Variable Arity

A function has fixed arity when you have to call it with the same number of arguments as the number of its parameters and a function has variable arity when you can call it with variable number of arguments, like functions with default parameters values.

>>> from typing import Any

>>> def fixed_arity(a: Any, b: Any) -> None:  # we have to call with 2 arguments
...     pass

>>> def variable_arity(a: Any, b: Any = None) -> None:  # we can call with 1 or 2 arguments
...     pass
>>>

Definitive Arity and Indefinite Arity

When a function can receive a finite number of arguments it has definitive arity, otherwise if the function can receive an undefined number of arguments it has indefinite arity. We can reproduce the indefinite arity using Python *args and **kwargs, see the example below:

>>> from typing import Any

>>> def definitive_arity(a: Any, b: Any = None) -> None: # we can call just with 1 or 2 arguments
...     pass

>>> def indefinite_arity(*args: Any, **kwargs: Any) -> None: # we can call with how many arguments we want
...     pass
>>>

Arguments vs Parameters

There is a little difference between arguments and parameters:

  • arguments: are the values that are passed to a function
  • parameters: are the variables in the function definition

Higher-Order Functions (HOF)

A function that takes a function as an argument and/or returns a function, basically we can treat functions as a value. In Python every function/method can be a Higher-Order Function.

The functions like reduce, map and filter are good examples of HOF, they receive a function as their first argument.

>>> from functools import reduce

>>> reduce(lambda accumulator, number: accumulator + number, [1, 2, 3])
6
>>>

We can create our own HOF, see the example below:

>>> from typing import Callable, TypeVar

>>> _ValueType = TypeVar('_ValueType')
>>> _ReturnType = TypeVar('_ReturnType')

>>> def get_transform_function() -> Callable[[str], int]:
...     return int

>>> def transform(
...     transform_function: Callable[[_ValueType], _ReturnType],
...     value_to_transform: _ValueType,
... ) -> _ReturnType:
...     return transform_function(value_to_transform)

>>> transform_function = get_transform_function()
>>> assert transform(transform_function, '42') == 42
>>>

IO

IO basically means Input/Output, but it is widely used to just tell that a function is impure.

We have a special type (IO) and a decorator (@impure) to do that in Python:

>>> import random
>>> from returns.io import IO, impure

>>> @impure
... def get_random_number() -> int:
...     return random.randint(0, 100)

>>> assert isinstance(get_random_number(), IO)
>>>

Further reading:

Closure (TODO)

A closure is a way of accessing a variable outside its scope. Formally, a closure is a technique for implementing lexically scoped named binding. It is a way of storing a function with an environment.

A closure is a scope which captures local variables of a function for access even after the execution has moved out of the block in which it is defined. ie. they allow referencing a scope after the block in which the variables were declared has finished executing.

# TODO

Lexical scoping is the reason why it is able to find the values of x and add - the private variables of the parent which has finished executing. This value is called a Closure.

The stack along with the lexical scope of the function is stored in form of reference to the parent. This prevents the closure and the underlying variables from being garbage collected(since there is at least one live reference to it).

Lambda Vs Closure: A lambda is essentially a function that is defined inline rather than the standard method of declaring functions. Lambdas can frequently be passed around as objects.

A closure is a function that encloses its surrounding state by referencing fields external to its body. The enclosed state remains across invocations of the closure.

Further reading/Sources

Partial Application

Partially applying a function means creating a new function by pre-filling some of the arguments to the original function. You can also use functools.partial or returns.curry.partial to partially apply a function in Python:

>>> from returns.curry import partial

>>> def takes_three_arguments(arg1: int, arg2: int, arg3: int) -> int:
...     return arg1 + arg2 + arg3

>>> assert partial(takes_three_arguments, 1, 2)(3) == 6
>>> assert partial(takes_three_arguments, 1)(2, 3) == 6
>>> assert partial(takes_three_arguments, 1, 2, 3)() == 6
>>>

The difference between returns.curry.partial and functools.partial is in how types are infered:

import functools

reveal_type(functools.partial(takes_three_arguments, 1))
# Revealed type is 'functools.partial[builtins.int*]'

reveal_type(partial(takes_three_arguments, 1))
# Revealed type is 'def (arg2: builtins.int, arg3: builtins.int) -> builtins.int'

Partial application helps create simpler functions from more complex ones by baking in data when you have it. Curried functions are automatically partially applied.

Further reading

Currying

The process of converting a function that takes multiple arguments into a function that takes them one at a time.

Each time the function is called it only accepts one argument and returns a function that takes one argument until all arguments are passed.

>>> from returns.curry import curry

>>> @curry
... def takes_three_args(a: int, b: int, c: int) -> int:
...     return a + b + c

>>> assert takes_three_args(1)(2)(3) == 6
>>>

Some implementations of curried functions can also take several of arguments instead of just a single argument:

>>> assert takes_three_args(1, 2)(3) == 6
>>> assert takes_three_args(1)(2, 3) == 6
>>> assert takes_three_args(1, 2, 3) == 6
>>>

Let's see what type takes_three_args has to get a better understanding of its features:

reveal_type(takes_three_args)

# Revealed type is:
# Overload(
#   def (a: builtins.int) -> Overload(
#     def (b: builtins.int, c: builtins.int) -> builtins.int, 
#     def (b: builtins.int) -> def (c: builtins.int) -> builtins.int
#   ), 
#   def (a: builtins.int, b: builtins.int) -> def (c: builtins.int) -> builtins.int, 
#   def (a: builtins.int, b: builtins.int, c: builtins.int) -> builtins.int
# )'

Further reading

Function Composition

For example, you can compose abs and int functions like so:

>>> assert abs(int('-1')) == 1
>>>

You can also create a third function that will have an input of the first one and an output of the second one:

>>> from typing import Callable, TypeVar

>>> _FirstType = TypeVar('_FirstType')
>>> _SecondType = TypeVar('_SecondType')
>>> _ThirdType = TypeVar('_ThirdType')

>>> def compose(
...     first: Callable[[_FirstType], _SecondType],
...     second: Callable[[_SecondType], _ThirdType],
... ) -> Callable[[_FirstType], _ThirdType]:
...     return lambda argument: second(first(argument))

>>> assert compose(int, abs)('-1') == 1
>>>

We already have this functions defined as returns.functions.compose!

>>> from returns.functions import compose
>>> assert compose(bool, str)([]) == 'False'
>>>

Further reading

Continuation (TODO)

At any given point in a program, the part of the code that's yet to be executed is known as a continuation.

# TODO

Continuations are often seen in asynchronous programming when the program needs to wait to receive data before it can continue. The response is often passed off to the rest of the program, which is the continuation, once it's been received.

# TODO

Point-Free Style

Point-Free is a style of writting code without using any intermediate variables.

Basically, you will end up with long chains of direct function calls. This style usually requires currying or other Higher-Order functions. This technique is also sometimes called "Tacit programming".

The most common example of Point-Free programming style is Unix with pipes:

ps aux | grep [k]de | gawk '{ print $2 }'

It also works for Python, let's say you have this function composition:

>>> str(bool(abs(-1)))
'True'
>>>

It might be problematic method methods on the first sight, because you need an instance to call a method on. But, you can always use HOF to fix that and compose normally:

>>> from returns.pipeline import flow
>>> from returns.pointfree import map_
>>> from returns.result import Success

>>> assert flow(
...     Success(-2),
...     map_(abs),
...     map_(range),
...     map_(list),
... ) == Success([0, 1])
>>>

Further reading:

Predicate

A predicate is a function that returns true or false for a given value. So, basically a predicate is an alias for Callable[[_ValueType], bool].

It is very useful when working with if, all, any, etc.

>>> def is_long(item: str) -> bool:
...     return len(item) > 3

>>> assert all(is_long(item) for item in ['1234', 'abcd'])
>>>

Futher reading

Contracts (TODO)

A contract specifies the obligations and guarantees of the behavior from a function or expression at runtime. This acts as a set of rules that are expected from the input and output of a function or expression, and errors are generally reported whenever a contract is violated.

# TODO

Category (TODO)

A category in category theory is a collection of objects and morphisms between them. In programming, typically types act as the objects and functions as morphisms.

To be a valid category 3 rules must be met:

  1. There must be an identity morphism that maps an object to itself. Where a is an object in some category, there must be a function from a -> a.
  2. Morphisms must compose. Where a, b, and c are objects in some category, and f is a morphism from a -> b, and g is a morphism from b -> c; g(f(x)) must be equivalent to (g • f)(x).
  3. Composition must be associative f • (g • h) is the same as (f • g) • h

Since these rules govern composition at very abstract level, category theory is great at uncovering new ways of composing things.

Further reading

Value (TODO)

Anything that can be assigned to a variable.

# TODO

Constant (TODO)

A variable that cannot be reassigned once defined.

# TODO

Constants are referentially transparent. That is, they can be replaced with the values that they represent without affecting the result.

# TODO

Lift (TODO)

Lifting is when you take a value and put it into an object like a Functor. If you lift a function into an Applicative Functor then you can make it work on values that are also in that functor.

Some implementations have a function called lift, or liftA2 to make it easier to run functions on functors.

# TODO

Lifting a one-argument function and applying it does the same thing as map.

# TODO

Referential Transparency (TODO)

An expression that can be replaced with its value without changing the behavior of the program is said to be referentially transparent.

Say we have function greet:

# TODO

Equational Reasoning (TODO)

When an application is composed of expressions and devoid of side effects, truths about the system can be derived from the parts.

Lambda (TODO)

An anonymous function that can be treated like a value.

def f(a):
  return a + 1

lambda a: a + 1

Lambdas are often passed as arguments to Higher-Order functions.

List([1, 2]).map(lambda x: x + 1) # [2, 3]

You can assign a lambda to a variable.

add1 = lambda a: a + 1

Lambda Calculus (TODO)

A branch of mathematics that uses functions to create a universal model of computation.

Lazy evaluation (TODO)

Lazy evaluation is a call-by-need evaluation mechanism that delays the evaluation of an expression until its value is needed. In functional languages, this allows for structures like infinite lists, which would not normally be available in an imperative language where the sequencing of commands is significant.

# TODO

Functor

An object that implements a map method which, while running over each value in the object to produce a new object, adheres to two rules:

Identity law

functor.map(lambda x: x) == functor

Associative law

functor.map(compose(f, g)) == functor.map(g).map(f)

Sometimes Functor can be called Mappable to its .map method. You can have a look at the real-life Functor interface:

>>> from typing import Callable, TypeVar
>>> from returns.interfaces.mappable import Mappable1 as Functor
>>> from returns.primitives.hkt import SupportsKind1

>>> _FirstType = TypeVar('_FirstType')
>>> _NewFirstType = TypeVar('_NewFirstType')

>>> class Box(SupportsKind1['Box', _FirstType], Functor[_FirstType]):
...     def __init__(self, inner_value: _FirstType) -> None:
...         self._inner_value = inner_value
...
...     def map(
...         self,
...         function: Callable[[_FirstType], _NewFirstType],
...     ) -> 'Box[_NewFirstType]':
...         return Box(function(self._inner_value))
...
...     def __eq__(self, other) -> bool:
...         return type(other) == type(self) and self._inner_value == other._inner_value

>>> assert Box(-5).map(abs) == Box(5)
>>>

Further reading:

Applicative Functor

An Applicative Functor is an object with apply and .from_value methods:

  • .apply applies a function in the object to a value in another object of the same type. Somethimes this method is also called ap
  • .from_value creates a new Applicative Functor from a pure value. Sometimes this method is also called pure

All Applicative Functors must also follow a bunch of laws.

Further reading:

Monoid

An object with a function that "combines" that object with another of the same type and an "empty" value, which can be added with no effect.

One simple monoid is the addition of numbers (with __add__ as an addition function and 0 as an empty element):

>>> assert 1 + 1 + 0 == 2
>>>

Tuples, lists, and strings are also monoids:

>>> assert (1,) + (2,) + () == (1, 2)
>>> assert [1] + [2] + [] == [1, 2]
>>> assert 'a' + 'b' + '' == 'ab'
>>>

Monad (TODO)

A monad is an Applicative Functor with bind method. bind is like map except it un-nests the resulting nested object.

# TODO

of is also known as return in other functional languages. chain is also known as flatmap and bind in other languages.

Comonad (TODO)

An object that has extract and extend functions.

# TODO

Morphism (TODO)

A transformation function.

Endomorphism (TODO)

A function where the input type is the same as the output.

# uppercase :: String -> String
uppercase = lambda s: s.upper() 

# decrement :: Number -> Number
decrement = lambda x: x - 1

Isomorphism (TODO)

A pair of transformations between 2 types of objects that is structural in nature and no data is lost.

# TODO

Homomorphism (TODO)

A homomorphism is just a structure preserving map. In fact, a functor is just a homomorphism between categories as it preserves the original category's structure under the mapping.

# TODO

Catamorphism (TODO)

A reduce_right function that applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

# TODO

Anamorphism (TODO)

An unfold function. An unfold is the opposite of fold (reduce). It generates a list from a single value.

# TODO

Hylomorphism (TODO)

The combination of anamorphism and catamorphism.

Paramorphism (TODO)

A function just like reduce_right. However, there's a difference:

In paramorphism, your reducer's arguments are the current value, the reduction of all previous values, and the list of values that formed that reduction.

# TODO

Apomorphism (TODO)

it's the opposite of paramorphism, just as anamorphism is the opposite of catamorphism. Whereas with paramorphism, you combine with access to the accumulator and what has been accumulated, apomorphism lets you unfold with the potential to return early.

Setoid (TODO)

An object that has an equals function which can be used to compare other objects of the same type.

Make array a setoid:

# TODO

Semigroup (TODO)

An object that has a concat function that combines it with another object of the same type.

# TODO

Foldable (TODO)

An object that has a reduce function that applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

# TODO

Lens (TODO)

A lens is a structure (often an object or function) that pairs a getter and a non-mutating setter for some other data structure.

# TODO

Lenses are also composable. This allows easy immutable updates to deeply nested data.

# TODO

Type Signatures (TODO)

Further reading

Algebraic data type (TODO)

A composite type made from putting other types together. Two common classes of algebraic types are sum and product.

Sum type (TODO)

A Sum type is the combination of two types together into another one. It is called sum because the number of possible values in the result type is the sum of the input types.

# TODO

Sum types are sometimes called union types, discriminated unions, or tagged unions.

The sumtypes library in Python helps with defining and using union types.

Product type (TODO)

A product type combines types together in a way you're probably more familiar with:

# TODO

See also Set theory.

Option (TODO)

Option is a sum type with two cases often called Some and None.

Option is useful for composing functions that might not return a value.

# TODO

Option is also known as Maybe. Some is sometimes called Just. None is sometimes called Nothing.

Function (TODO)

A function f :: A => B is an expression - often called arrow or lambda expression - with exactly one (immutable) parameter of type A and exactly one return value of type B. That value depends entirely on the argument, making functions context-independent, or referentially transparent. What is implied here is that a function must not produce any hidden side effects - a function is always pure, by definition. These properties make functions pleasant to work with: they are entirely deterministic and therefore predictable. Functions enable working with code as data, abstracting over behaviour:

# TODO

Partial function (TODO)

A partial function is a function which is not defined for all arguments - it might return an unexpected result or may never terminate. Partial functions add cognitive overhead, they are harder to reason about and can lead to runtime errors. Some examples:

# TODO

Dealing with partial functions (TODO)

Partial functions are dangerous as they need to be treated with great caution. You might get an unexpected (wrong) result or run into runtime errors. Sometimes a partial function might not return at all. Being aware of and treating all these edge cases accordingly can become very tedious. Fortunately a partial function can be converted to a regular (or total) one. We can provide default values or use guards to deal with inputs for which the (previously) partial function is undefined. Utilizing the Option type, we can yield either Some(value) or None where we would otherwise have behaved unexpectedly:

# TODO