ArticleslgStudy

mathematics

Function composition (computer science)

Function composition (computer science) is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Function composition (computer science) rather than just read about it. In short: In computer science, function composition is an act or mechanism to combine simple functions to build more complex ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole.

Key takeaways

  • Function composition (computer science) belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Function composition (computer science) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Function composition (computer science) from memory before moving on to harder problems.

Reference excerpt

In computer science, function composition is an act or mechanism to combine simple functions to build more complex ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole. Programmers frequently apply functions to results of other functions, and almost all programming languages allow it. In some cases, the composition of functions is interesting as a function in its own right, to be used later. Such a function can always be defined but languages with first-class functions make it easier. The ability to easily compose functions encourages factoring (breaking apart) functions for maintainability and code reuse. More generally, big systems might be built by composing whole programs. Narrowly speaking, function composition applies to functions that operate on a finite amount of data, each step sequentially processing it before handing it to the next. Functions that operate on potentially infinite data (a stream or other codata) are known as filters, and are instead connected in a pipeline, which is analogous to function composition and can execute concurrently.

Composing function calls For example, suppose we have two functions f and g, as in z = f(y) and y = g(x). Composing them means we first compute y = g(x), and then use y to compute z = f(y). Here is the example in the C language:

The steps can be combined if we don't give a name to the intermediate result:

Despite differences in length, these two implementations compute the same result. The second implementation requires only one line of code and is colloquially referred to as a "highly composed" form. Readability and hence maintainability is one advantage of highly composed forms, since they require fewer lines of code, minimizing a program's "surface area". DeMarco and Lister empirically verify an inverse relationship between surface area and maintainability. On the other hand, it may be possible to overuse highly composed forms. A nesting of too many functions may have the opposite effect, making the code less maintainable. In a stack-based language, functional composition is even more natural: it is performed by concatenation, and is usually the primary method of program design. The above example in Forth:

g f

Which will take whatever was on the stack before, apply g, then f, and leave the result on the stack. See postfix composition notation for the corresponding mathematical notation.

Naming the composition of functions Now suppose that the combination of calling f() on the result of g() is frequently useful, and which we want to name foo() to be used as a function in its own right. In most languages, we can define a new function implemented by composition. Example in C:

(the long form with intermediates would work as well.) Example in Forth:

: foo g f ;

In languages such as C, the only way to create a new function is to define it in the program source, which means that functions can't be composed at run time. An evaluation of an arbitrary composition of predefined functions, however, is possible:

First-class composition In functional programming languages, function composition can be naturally expressed as a higher-order function or operator. In other programming languages you can write your own mechanisms to perform function composition.

Haskell In Haskell, the example foo = f  ∘  g given above becomes:

foo = f . g

using the built-in composition operator (.) which can be read as f after g or g composed with f. The composition operator  ∘   itself can be defined in Haskell using a lambda expression:

The first line describes the type of (.) - it takes a pair of functions, f,  g and returns a function (the lambda expression on the second line). Note that Haskell doesn't require specification of the exact input and output types of f and g; the a, b, c, and x are placeholders; only the relation between f,  g matters (f must accept what g returns). This makes (.) a polymorphic operator. A polymorphic operator with multiple arguments, in Haskell's point-free notation

is where the parameter names are immaterial, as stated above. Michal Ševčík credits CScalfani with a systematic exposition for function composition using additional parameters.

Lisp Variants of Lisp, especially Scheme, the interchangeability of code and data together with the treatment of functions lend themselves extremely well for a recursive definition of a variadic compositional operator.

APL Many dialects of APL feature built in function composition using the symbol ∘. This higher-order function extends function composition to dyadic application of the left side function such that A f∘g B is A f g B.

Additionally, you can define function composition:

In dialect that does not support inline definition using braces, the traditional definition is available:

Raku Raku like Haskell has a built in function composition operator, the main difference is it is spelled as ∘ or o.

Also like Haskell you could define the operator yourself. In fact the following is the Raku code used to define it in the Rakudo implementation.

Nim Nim supports uniform function call syntax, which allows for arbitrary function composition through the method syntax . operator.

Python In Python, a way to define the composition for any group of functions, is using functools.reduce function:

JavaScript In JavaScript we can define it as a function which takes two functions f and g, and produces a function:

C# In C# we can define it as an Extension method which takes Funcs f and g, and produces a new Func:

Ruby Languages like Ruby let you construct a binary operator yourself:

However, a native function composition operator was introduced in Ruby 2.6:

Research survey Notions of composition, including the principle of compositionality and composability, are so ubiquitous that numerous strands of research have separately evolved. The following is a sampling of the kind of research in which the notion of composition is central.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Function composition (computer science)

Start with the simplest possible case. Write down what Function composition (computer science) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Function composition (computer science) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Function composition (computer science) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Function composition (computer science)

In research
Function composition (computer science) appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Function composition (computer science) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Function composition (computer science) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Higher-order functions, Programming language topics, so understanding it makes those chapters shorter.
In everyday life
Look for Function composition (computer science) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Function composition (computer science)” →

How to study Function composition (computer science) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Function composition (computer science) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Function composition (computer science) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Function composition (computer science) in simple terms?

In computer science, function composition is an act or mechanism to combine simple functions to build more complex ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the who…

Why does Function composition (computer science) matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Function composition (computer science)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Function composition (computer science).

Tags

  • Higher-order functions
  • Programming language topics

Keep exploring