Skip to content

Instantly share code, notes, and snippets.

@iancrowther
Created October 23, 2015 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iancrowther/0e4442109e28d7d02989 to your computer and use it in GitHub Desktop.
Save iancrowther/0e4442109e28d7d02989 to your computer and use it in GitHub Desktop.
The distinction between anonymous functions and lambdas in JavaScript.
// TL;DR - Lambda means "function used as data".
// Anonymous function means "function without a name".
// This is one of the relatively few cases where the Wikipedia definition of
// a word, while not entirely wrong, is misleading. Lambdas and anonymous
// functions are distinct ideas.
// These ideas are commonly confused because in many programming languages
// all lambdas are anonymous or vise verse.
// In JavaScript, not all lambdas are anonymous, and not all anonymous
// functions are lambdas, so the distinction has some practical meaning.
(function () {
console.log(`
Some people mistakenly think that "lambda" and "anonymous function" have
the same meaning. Let's clear that up.
In computer science, the most important, defining characteristic of a lambda
expression is that it is used as data. That is, the function is passed as
an argument to another function, returned as a value from a function, or
assigned to variables or data structures.
This is basically the definition of a first class function. In JavaScript, you
can use any function as a lambda expression because functions are first class.
However, that does not mean that all JavaScript functions are therefore lambda
expressions. For instance, this expression defines a function which gets
immediately invoked and then dropped on the floor rather than passed,
exported, or assigned. In other words, the function is not used as data.
Instead, it's used for a side-effect (logging this text to the console).
Conceptually, this is more akin to imperative programming than functional
programming, and thinking of this function as a lambda would add zero useful
meaning to your understanding of it.
This distinction is not essential, but is a useful concept when you're
learning functional programming. In other words, if you understand this
distinction, you have a deeper understanding of what the word "lambda" means
in both functional programming and lambda calculus (which are closely
related).
It is possible to use functional language features like first-class functions
in an imperative style, but that adds nothing interesting to your grasp of the
language, or your grasp of why lambda expressions exist in the first place.
`)
})();
$('#el').on('click', function clickHandler () {
console.log(`
This is an example of a lambda expression that is not anonymous. As you can
see, it clearly has a name, clickHandler, which can be used inside the
function for the purpose of recursion (also an important concept in functional
programming).
It is a lambda expression because of the semantic use -- it's being passed
to another function as data. The .on() function is using the function as
an argument -- in other words, communicated as a message (i.e. functions as
data).
`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment