diff options
| author | Eli Zaretskii | 2008-10-19 14:57:50 +0000 |
|---|---|---|
| committer | Eli Zaretskii | 2008-10-19 14:57:50 +0000 |
| commit | 80f85d7cda750c0ee0c4e0a2f9646b27c37fb52f (patch) | |
| tree | aaa38c59e9e6a6a639d3ba68851adae31a718ed9 | |
| parent | d24ae8f0576120bc5c4cef7e7651d1d53684466b (diff) | |
| download | emacs-80f85d7cda750c0ee0c4e0a2f9646b27c37fb52f.tar.gz emacs-80f85d7cda750c0ee0c4e0a2f9646b27c37fb52f.zip | |
(Calling Functions): Document `apply-partially'.
| -rw-r--r-- | doc/lispref/ChangeLog | 2 | ||||
| -rw-r--r-- | doc/lispref/functions.texi | 35 | ||||
| -rw-r--r-- | etc/NEWS | 1 |
3 files changed, 38 insertions, 0 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 55343fcbdf7..ffb7b48cb4c 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | 2008-10-19 Eli Zaretskii <eliz@gnu.org> | 1 | 2008-10-19 Eli Zaretskii <eliz@gnu.org> |
| 2 | 2 | ||
| 3 | * functions.texi (Calling Functions): Document `apply-partially'. | ||
| 4 | |||
| 3 | * hooks.texi (Standard Hooks): Mention | 5 | * hooks.texi (Standard Hooks): Mention |
| 4 | `before-hack-local-variables-hook' and `hack-local-variables-hook'. | 6 | `before-hack-local-variables-hook' and `hack-local-variables-hook'. |
| 5 | 7 | ||
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi index 068d05ffc99..4609fc18cef 100644 --- a/doc/lispref/functions.texi +++ b/doc/lispref/functions.texi | |||
| @@ -725,6 +725,41 @@ For an interesting example of using @code{apply}, see @ref{Definition | |||
| 725 | of mapcar}. | 725 | of mapcar}. |
| 726 | @end defun | 726 | @end defun |
| 727 | 727 | ||
| 728 | @cindex partial application of functions | ||
| 729 | @cindex currying | ||
| 730 | Sometimes, it is useful to fix some of the function's arguments at | ||
| 731 | certain values, and leave the rest of arguments for when the function | ||
| 732 | is actually called. The act of fixing some of the function's | ||
| 733 | arguments is called @dfn{partial application} of the function@footnote{ | ||
| 734 | This is related to, but different from @dfn{currying}, which | ||
| 735 | transforms a function that takes multiple arguments in such a way that | ||
| 736 | it can be called as a chain of functions, each one with a single | ||
| 737 | argument.}. | ||
| 738 | The result is a new function that accepts the rest of | ||
| 739 | arguments and calls the original function with all the arguments | ||
| 740 | combined. Emacs provides a function for partial evaluation: | ||
| 741 | |||
| 742 | @defun apply-partially func &rest args | ||
| 743 | This function returns a new function which, when called, will call | ||
| 744 | @var{func} with the list of arguments composed from @var{args} and | ||
| 745 | additional arguments specified at the time of the call. If @var{func} | ||
| 746 | accepts @var{n} arguments, then a call to @code{apply-partially} with | ||
| 747 | @w{@code{@var{m} < @var{n}}} arguments will produce a new function of | ||
| 748 | @w{@code{@var{n} - @var{m}}} arguments. | ||
| 749 | |||
| 750 | Here's an example of using @code{apply-partially} to produce a | ||
| 751 | function @code{incr}, that will increment its argument by one, based | ||
| 752 | on the Emacs Lisp primitive @code{+}: | ||
| 753 | |||
| 754 | @example | ||
| 755 | (fset 'incr (apply-partially '+ 1)) | ||
| 756 | @group | ||
| 757 | (incr 10) | ||
| 758 | @result{} 11 | ||
| 759 | @end group | ||
| 760 | @end example | ||
| 761 | @end defun | ||
| 762 | |||
| 728 | @cindex functionals | 763 | @cindex functionals |
| 729 | It is common for Lisp functions to accept functions as arguments or | 764 | It is common for Lisp functions to accept functions as arguments or |
| 730 | find them in data structures (especially in hook variables and property | 765 | find them in data structures (especially in hook variables and property |
| @@ -1581,6 +1581,7 @@ times the default column width. | |||
| 1581 | *** `format-seconds' converts a number of seconds into a readable | 1581 | *** `format-seconds' converts a number of seconds into a readable |
| 1582 | string of days, hours, etc. | 1582 | string of days, hours, etc. |
| 1583 | 1583 | ||
| 1584 | +++ | ||
| 1584 | *** `apply-partially' performs a "curried" application of a function. | 1585 | *** `apply-partially' performs a "curried" application of a function. |
| 1585 | 1586 | ||
| 1586 | *** `read-shell-command' does what its name says, with completion. It | 1587 | *** `read-shell-command' does what its name says, with completion. It |