elisp: Anonymous Functions
12.7 Anonymous Functions
========================
Although functions are usually defined with ‘defun’ and given names at
the same time, it is sometimes convenient to use an explicit lambda
expression—an “anonymous function”. Anonymous functions are valid
wherever function names are. They are often assigned as variable
values, or as arguments to functions; for instance, you might pass one
as the FUNCTION argument to ‘mapcar’, which applies that function to
DONTPRINTYET each element of a list (Mapping Functions). *NoteDONTPRINTYET each element of a list (Mapping Functions).
describe-symbols example, for a realistic example of this.
When defining a lambda expression that is to be used as an anonymous
function, you can in principle use any method to construct the list.
But typically you should use the ‘lambda’ macro, or the ‘function’
special form, or the ‘#'’ read syntax:
-- Macro: lambda args [doc] [interactive] body...
This macro returns an anonymous function with argument list ARGS,
documentation string DOC (if any), interactive spec INTERACTIVE (if
any), and body forms given by BODY.
In effect, this macro makes ‘lambda’ forms self-quoting: evaluating
a form whose CAR is ‘lambda’ yields the form itself:
(lambda (x) (* x x))
⇒ (lambda (x) (* x x))
The ‘lambda’ form has one other effect: it tells the Emacs
evaluator and byte-compiler that its argument is a function, by
using ‘function’ as a subroutine (see below).
-- Special Form: function function-object
This special form returns FUNCTION-OBJECT without evaluating it.
In this, it is similar to ‘quote’ (Quoting). But unlike
‘quote’, it also serves as a note to the Emacs evaluator and
byte-compiler that FUNCTION-OBJECT is intended to be used as a
function. Assuming FUNCTION-OBJECT is a valid lambda expression,
this has two effects:
• When the code is byte-compiled, FUNCTION-OBJECT is compiled
into a byte-code function object (Byte Compilation).
• When lexical binding is enabled, FUNCTION-OBJECT is converted
into a closure. Closures.
The read syntax ‘#'’ is a short-hand for using ‘function’. The
following forms are all equivalent:
(lambda (x) (* x x))
(function (lambda (x) (* x x)))
#'(lambda (x) (* x x))
In the following example, we define a ‘change-property’ function that
takes a function as its third argument, followed by a ‘double-property’
function that makes use of ‘change-property’ by passing it an anonymous
function:
(defun change-property (symbol prop function)
(let ((value (get symbol prop)))
(put symbol prop (funcall function value))))
(defun double-property (symbol prop)
(change-property symbol prop (lambda (x) (* 2 x))))
Note that we do not quote the ‘lambda’ form.
If you compile the above code, the anonymous function is also
compiled. This would not happen if, say, you had constructed the
anonymous function by quoting it as a list:
(defun double-property (symbol prop)
(change-property symbol prop '(lambda (x) (* 2 x))))
In that case, the anonymous function is kept as a lambda expression in
the compiled code. The byte-compiler cannot assume this list is a
function, even though it looks like one, since it does not know that
‘change-property’ intends to use it as a function.