ArticleslgStudy

mathematics

Anonymous function

Anonymous function 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 Anonymous function rather than just read about it. In short: In computer programming, an anonymous function (function literal, lambda function, or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function.

Key takeaways

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

Reference excerpt

In computer programming, an anonymous function (function literal, lambda function, or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types. Anonymous functions originate in the work of Alonzo Church in his invention of the lambda calculus, in which all functions are anonymous, in 1936, before electronic computers. In several programming languages, anonymous functions are introduced using the keyword lambda, and anonymous functions are often referred to as lambdas or lambda abstractions. Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions.

Names The names "lambda abstraction", "lambda function", and "lambda expression" refer to the notation of function abstraction in lambda calculus, where the usual function f(x)=M would be written (λx.M), and where M is an expression that uses x. Compare to the Python syntax of lambda x: M. The name "arrow function" refers to the mathematical "maps to" symbol, x↦M. Compare to the JavaScript syntax of x => M.

Uses

Anonymous functions can encapsulate functionality that does not require naming and is intended for short-term or localized use. Some notable examples include closures and currying. The use of anonymous functions is a matter of style. Using them is never the only way to solve a problem; each anonymous function could instead be defined as a named function and called by name. Anonymous functions often provide a briefer notation than defining named functions. In languages that do not permit the definition of named functions in local scopes, anonymous functions may provide encapsulation via localized scope, however the code in the body of such anonymous function may not be re-usable, or amenable to separate testing. Short/simple anonymous functions used in expressions may be easier to read and understand than separately defined named functions, though lacking a descriptive name may reduce code readability. In some programming languages, anonymous functions are commonly implemented for very specific purposes such as binding events to callbacks or instantiating the function for particular values, which may be more efficient in a dynamic programming language, more readable, and less error-prone than calling a named function. The following examples are written in Python 3.

Sorting Many languages provide a generic function that sorts a list (or array) of objects into an order determined by a comparison function which compares two objects to determine if they are equal or if one is greater or less than the other. Using an anonymous comparison function expression passed as an argument to a generic sort function is often more concise than creating a named comparison function. Consider this Python code sorting a list of strings by length of the string:

The anonymous function in this example is the lambda expression:

The anonymous function accepts one argument, x, and returns the length of its argument, which is then used by the sort() method as the criteria for sorting. Basic syntax of a lambda function in Python is

The expression returned by the lambda function can be assigned to a variable and used in the code at multiple places.

Another example would be sorting items in a list by the name of their class (in Python, everything has a class):

Note that 11.2 has class name "float", 10 has class name "int", and 'number' has class name "str". The sorted order is "float", "int", then "str".

Closures

Closures are functions evaluated in an environment containing bound variables. The following example binds the variable "threshold" within an anonymous function that compares input values to this threshold.

This can be used as a sort of generator of comparison functions:

It would be impractical to create a function for every possible comparison function and may be too inconvenient to keep the threshold around for further use. Regardless of the reason why a closure is used, the anonymous function is the entity that contains the functionality that does the comparing.

Currying

Currying transforms a function that takes multiple arguments into a sequence of functions each accepting a single argument. In this example, a function that performs division by any integer is transformed into one that performs division by a set integer.

While the use of anonymous functions is perhaps not common with currying, it still can be used. In the above example, the function divisor generates functions with a specified divisor. The functions half and third curry the divide function with a fixed divisor. The divisor function also forms a closure by binding the variable d.

Higher-order functions A higher-order function is a function that takes a function as an argument or returns one as a result. This technique is frequently employed to tailor the behavior of a generically defined function, such as a loop or recursion pattern. Anonymous functions are a convenient way to specify such function arguments. The following examples are in Python 3.

Map

The map function performs a function call on each element of a list. The following example squares every element in an array with an anonymous function.

The anonymous function takes an argument and returns its square. The above form is discouraged by the creators of the language, who maintain that the form presented below has the same meaning and is more aligned with the philosophy of the language:

Filter

The filter function returns all elements from a list that evaluate True when passed to a certain function.

The anonymous function checks if the argument passed to it is even. The same as with map, the form below is considered more appropriate:

Fold

A fold function runs over all elements in a structure (for lists usually left-to-right, a "left fold", called reduce in Python), accumulating a value as it goes. This can be used to combine all elements of a structure into one value, for example:

This performs

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Anonymous function

Start with the simplest possible case. Write down what Anonymous function 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 Anonymous function 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 Anonymous function 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 Anonymous function

In research
Anonymous function 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 Anonymous function 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
Anonymous function is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Functional programming, Lambda calculus, so understanding it makes those chapters shorter.
In everyday life
Look for Anonymous function 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 “Anonymous function” →

How to study Anonymous function in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Anonymous function 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 Anonymous function out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Anonymous function in simple terms?

In computer programming, an anonymous function (function literal, lambda function, or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function tha…

Why does Anonymous function 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 Anonymous function?

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 Anonymous function.

Tags

  • Data types
  • Functional programming
  • Lambda calculus
  • Subroutines

Keep exploring