diff options
| author | Eli Zaretskii | 2018-11-03 15:11:33 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2018-11-03 15:11:33 +0200 |
| commit | e824c914dabd92537a0d6e44eaa10bb4699c312f (patch) | |
| tree | c867f703cbf7b34400201b39af1fcf69a23ee1c0 | |
| parent | 74bc0e16b7f9fdc5011c28182a2c8d828ee426d8 (diff) | |
| download | emacs-e824c914dabd92537a0d6e44eaa10bb4699c312f.tar.gz emacs-e824c914dabd92537a0d6e44eaa10bb4699c312f.zip | |
Improve documentation of destructuring-binding macros
* lisp/emacs-lisp/pcase.el (pcase-dolist, pcase-let)
(pcase-let*): Improve the doc strings.
* doc/lispref/sequences.texi (Sequence Functions): Improve
wording and rename arguments of seq-let to be more
descriptive. Add a cross-reference to "Destructuring with
pcase Patterns".
* doc/lispref/control.texi (Pattern-Matching Conditional):
Improve wording and the menu.
(pcase Macro): Incorporate patch suggested by Paul Eggert
<eggert@cs.ucla.edu>. Reformat text.
(Destructuring with pcase Patterns): Rename from
"Destructuring patterns", and improve wording and indexing.
| -rw-r--r-- | doc/lispref/control.texi | 228 | ||||
| -rw-r--r-- | doc/lispref/sequences.texi | 20 | ||||
| -rw-r--r-- | lisp/emacs-lisp/pcase.el | 43 |
3 files changed, 160 insertions, 131 deletions
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index 06c6622bf01..f80622e6025 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi | |||
| @@ -419,65 +419,68 @@ This is not completely equivalent because it can evaluate @var{arg1} or | |||
| 419 | @node Pattern-Matching Conditional | 419 | @node Pattern-Matching Conditional |
| 420 | @section Pattern-Matching Conditional | 420 | @section Pattern-Matching Conditional |
| 421 | @cindex pcase | 421 | @cindex pcase |
| 422 | @cindex pattern matching | 422 | @cindex pattern matching, programming style |
| 423 | 423 | ||
| 424 | Aside from the four basic conditional forms, Emacs Lisp also | 424 | Aside from the four basic conditional forms, Emacs Lisp also |
| 425 | has a pattern-matching conditional form, the @code{pcase} macro, | 425 | has a pattern-matching conditional form, the @code{pcase} macro, |
| 426 | a hybrid of @code{cond} and @code{cl-case} | 426 | a hybrid of @code{cond} and @code{cl-case} |
| 427 | (@pxref{Conditionals,,,cl,Common Lisp Extensions}) | 427 | (@pxref{Conditionals,,,cl,Common Lisp Extensions}) |
| 428 | that overcomes their limitations and introduces | 428 | that overcomes their limitations and introduces |
| 429 | the @dfn{pattern matching} programming style. | 429 | the @dfn{pattern matching programming style}. |
| 430 | First, the limitations: | 430 | The limitations that @code{pcase} overcomes are: |
| 431 | 431 | ||
| 432 | @itemize | 432 | @itemize |
| 433 | @item The @code{cond} form chooses among alternatives | 433 | @item |
| 434 | by evaluating the predicate @var{condition} of each | 434 | The @code{cond} form chooses among alternatives by evaluating the |
| 435 | of its clauses (@pxref{Conditionals}). | 435 | predicate @var{condition} of each of its clauses |
| 436 | The primary limitation is that variables let-bound in @var{condition} | 436 | (@pxref{Conditionals}). The primary limitation is that variables |
| 437 | are not available to the clause's @var{body-forms}. | 437 | let-bound in @var{condition} are not available to the clause's |
| 438 | @var{body-forms}. | ||
| 438 | 439 | ||
| 439 | Another annoyance (more an inconvenience than a limitation) | 440 | Another annoyance (more an inconvenience than a limitation) |
| 440 | is that when a series of @var{condition} predicates implement | 441 | is that when a series of @var{condition} predicates implement |
| 441 | equality tests, there is a lot of repeated code. | 442 | equality tests, there is a lot of repeated code. (@code{cl-case} |
| 442 | For that, why not use @code{cl-case}? | 443 | solves this inconvenience.) |
| 443 | 444 | ||
| 444 | @item | 445 | @item |
| 445 | The @code{cl-case} macro chooses among alternatives by evaluating | 446 | The @code{cl-case} macro chooses among alternatives by evaluating |
| 446 | the equality of its first argument against a set of specific | 447 | the equality of its first argument against a set of specific |
| 447 | values. | 448 | values. |
| 448 | The limitations are two-fold: | 449 | |
| 450 | Its limitations are two-fold: | ||
| 449 | 451 | ||
| 450 | @enumerate | 452 | @enumerate |
| 451 | @item The equality tests use @code{eql}. | 453 | @item |
| 452 | @item The values must be known and written in advance. | 454 | The equality tests use @code{eql}. |
| 455 | @item | ||
| 456 | The values must be known and written in advance. | ||
| 453 | @end enumerate | 457 | @end enumerate |
| 454 | 458 | ||
| 455 | @noindent | 459 | @noindent |
| 456 | These render @code{cl-case} unsuitable for strings or compound | 460 | These render @code{cl-case} unsuitable for strings or compound |
| 457 | data structures (e.g., lists or vectors). | 461 | data structures (e.g., lists or vectors). (@code{cond} doesn't have |
| 458 | For that, why not use @code{cond}? | 462 | these limitations, but it has others, see above.) |
| 459 | (And here we end up in a circle.) | ||
| 460 | @end itemize | 463 | @end itemize |
| 461 | 464 | ||
| 462 | @noindent | 465 | @noindent |
| 463 | Conceptually, the @code{pcase} macro borrows the first-arg focus | 466 | Conceptually, the @code{pcase} macro borrows the first-arg focus |
| 464 | of @code{cl-case} and the clause-processing flow of @code{cond}, | 467 | of @code{cl-case} and the clause-processing flow of @code{cond}, |
| 465 | replacing @var{condition} with a generalization of | 468 | replacing @var{condition} with a generalization of |
| 466 | the equality test called @dfn{matching}, | 469 | the equality test which is a variant of @dfn{pattern matching}, |
| 467 | and adding facilities so that you can concisely express a | 470 | and adding facilities so that you can concisely express a |
| 468 | clause's predicate, and arrange to share let-bindings between | 471 | clause's predicate, and arrange to share let-bindings between |
| 469 | a clause's predicate and @var{body-forms}. | 472 | a clause's predicate and @var{body-forms}. |
| 470 | 473 | ||
| 471 | The concise expression of a predicate is known as a @dfn{pattern}. | 474 | The concise expression of a predicate is known as a @dfn{pattern}. |
| 472 | When the predicate, called on the value of the first arg, | 475 | When the predicate, called on the value of the first arg, returns |
| 473 | returns non-@code{nil}, the pattern matches the value | 476 | non-@code{nil}, we say that ``the pattern matches the value'' (or |
| 474 | (or sometimes ``the value matches the pattern''). | 477 | sometimes ``the value matches the pattern''). |
| 475 | 478 | ||
| 476 | @menu | 479 | @menu |
| 477 | * The @code{pcase} macro: pcase Macro. Plus examples and caveats. | 480 | * The @code{pcase} macro: pcase Macro. Includes examples and caveats. |
| 478 | * Extending @code{pcase}: Extending pcase. Define new kinds of patterns. | 481 | * Extending @code{pcase}: Extending pcase. Define new kinds of patterns. |
| 479 | * Backquote-Style Patterns: Backquote Patterns. Structural matching. | 482 | * Backquote-Style Patterns: Backquote Patterns. Structural patterns matching. |
| 480 | * Destructuring patterns:: Using pcase patterns to extract subfields. | 483 | * Destructuring with pcase Patterns:: Using pcase patterns to extract subfields. |
| 481 | @end menu | 484 | @end menu |
| 482 | 485 | ||
| 483 | @node pcase Macro | 486 | @node pcase Macro |
| @@ -498,30 +501,30 @@ of the last of @var{body-forms} in the successful clause. | |||
| 498 | Otherwise, @code{pcase} evaluates to @code{nil}. | 501 | Otherwise, @code{pcase} evaluates to @code{nil}. |
| 499 | @end defmac | 502 | @end defmac |
| 500 | 503 | ||
| 501 | Each @var{pattern} has to be a @dfn{pcase pattern}, which can either | 504 | @cindex pcase pattern |
| 502 | use one of the core patterns defined below, or use one of the patterns | 505 | Each @var{pattern} has to be a @dfn{pcase pattern}, which can use |
| 503 | defined via @code{pcase-defmacro}. | 506 | either one of the core patterns defined below, or one of the patterns |
| 507 | defined via @code{pcase-defmacro} (@pxref{Extending pcase}). | ||
| 504 | 508 | ||
| 505 | The rest of this subsection | 509 | The rest of this subsection describes different forms of core |
| 506 | describes different forms of core patterns, | 510 | patterns, presents some examples, and concludes with important caveats |
| 507 | presents some examples, | 511 | on using the let-binding facility provided by some pattern forms. A |
| 508 | and concludes with important caveats on using the | 512 | core pattern can have the following forms: |
| 509 | let-binding facility provided by some pattern forms. | ||
| 510 | A core pattern can have the following forms: | ||
| 511 | 513 | ||
| 512 | @table @code | 514 | @table @code |
| 513 | 515 | ||
| 514 | @item _ | 516 | @item _ |
| 515 | Matches any @var{expval}. | 517 | Matches any @var{expval}. |
| 516 | This is known as @dfn{don't care} or @dfn{wildcard}. | 518 | This is also known as @dfn{don't care} or @dfn{wildcard}. |
| 517 | 519 | ||
| 518 | @item '@var{val} | 520 | @item '@var{val} |
| 519 | Matches if @var{expval} is @code{equal} to @var{val}. | 521 | Matches if @var{expval} is equals @var{val}. The comparison is done |
| 522 | as if by @code{equal} (@pxref{Equality Predicates}). | ||
| 520 | 523 | ||
| 521 | @item @var{keyword} | 524 | @item @var{keyword} |
| 522 | @itemx @var{integer} | 525 | @itemx @var{integer} |
| 523 | @itemx @var{string} | 526 | @itemx @var{string} |
| 524 | Matches if @var{expval} is @code{equal} to the literal object. | 527 | Matches if @var{expval} equals the literal object. |
| 525 | This is a special case of @code{'@var{val}}, above, | 528 | This is a special case of @code{'@var{val}}, above, |
| 526 | possible because literal objects of these types are self-quoting. | 529 | possible because literal objects of these types are self-quoting. |
| 527 | 530 | ||
| @@ -533,17 +536,17 @@ Matches any @var{expval}, and additionally let-binds @var{symbol} to | |||
| 533 | If @var{symbol} is part of a sequencing pattern @var{seqpat} | 536 | If @var{symbol} is part of a sequencing pattern @var{seqpat} |
| 534 | (e.g., by using @code{and}, below), the binding is also available to | 537 | (e.g., by using @code{and}, below), the binding is also available to |
| 535 | the portion of @var{seqpat} following the appearance of @var{symbol}. | 538 | the portion of @var{seqpat} following the appearance of @var{symbol}. |
| 536 | This usage has some caveats (@pxref{pcase-symbol-caveats,,caveats}). | 539 | This usage has some caveats, see @ref{pcase-symbol-caveats,,caveats}. |
| 537 | 540 | ||
| 538 | Two symbols to avoid are @code{t}, which behaves like @code{_} | 541 | Two symbols to avoid are @code{t}, which behaves like @code{_} |
| 539 | (above) and is deprecated, and @code{nil}, which signals error. | 542 | (above) and is deprecated, and @code{nil}, which signals an error. |
| 540 | Likewise, it makes no sense to bind keyword symbols | 543 | Likewise, it makes no sense to bind keyword symbols |
| 541 | (@pxref{Constant Variables}). | 544 | (@pxref{Constant Variables}). |
| 542 | 545 | ||
| 543 | @item (pred @var{function}) | 546 | @item (pred @var{function}) |
| 544 | Matches if the predicate @var{function} returns non-@code{nil} | 547 | Matches if the predicate @var{function} returns non-@code{nil} |
| 545 | when called on @var{expval}. | 548 | when called on @var{expval}. |
| 546 | @var{function} can have one of the possible forms: | 549 | the predicate @var{function} can have one of the following forms: |
| 547 | 550 | ||
| 548 | @table @asis | 551 | @table @asis |
| 549 | @item function name (a symbol) | 552 | @item function name (a symbol) |
| @@ -570,20 +573,17 @@ the actual function call becomes: @w{@code{(= 42 @var{expval})}}. | |||
| 570 | @item (app @var{function} @var{pattern}) | 573 | @item (app @var{function} @var{pattern}) |
| 571 | Matches if @var{function} called on @var{expval} returns a | 574 | Matches if @var{function} called on @var{expval} returns a |
| 572 | value that matches @var{pattern}. | 575 | value that matches @var{pattern}. |
| 573 | @var{function} can take one of the | 576 | @var{function} can take one of the forms described for @code{pred}, |
| 574 | forms described for @code{pred}, above. | 577 | above. Unlike @code{pred}, however, @code{app} tests the result |
| 575 | Unlike @code{pred}, however, | 578 | against @var{pattern}, rather than against a boolean truth value. |
| 576 | @code{app} tests the result against @var{pattern}, | ||
| 577 | rather than against a boolean truth value. | ||
| 578 | 579 | ||
| 579 | @item (guard @var{boolean-expression}) | 580 | @item (guard @var{boolean-expression}) |
| 580 | Matches if @var{boolean-expression} evaluates to non-@code{nil}. | 581 | Matches if @var{boolean-expression} evaluates to non-@code{nil}. |
| 581 | 582 | ||
| 582 | @item (let @var{pattern} @var{expr}) | 583 | @item (let @var{pattern} @var{expr}) |
| 583 | Evaluates @var{expr} to get @var{exprval} | 584 | Evaluates @var{expr} to get @var{exprval} and matches if @var{exprval} |
| 584 | and matches if @var{exprval} matches @var{pattern}. | 585 | matches @var{pattern}. (It is called @code{let} because @var{pattern} |
| 585 | (It is called @code{let} because | 586 | can bind symbols to values using @var{symbol}.) |
| 586 | @var{pattern} can bind symbols to values using @var{symbol}.) | ||
| 587 | @end table | 587 | @end table |
| 588 | 588 | ||
| 589 | @cindex sequencing pattern | 589 | @cindex sequencing pattern |
| @@ -596,18 +596,16 @@ but instead of processing values, they process sub-patterns. | |||
| 596 | 596 | ||
| 597 | @table @code | 597 | @table @code |
| 598 | @item (and @var{pattern1}@dots{}) | 598 | @item (and @var{pattern1}@dots{}) |
| 599 | Attempts to match @var{pattern1}@dots{}, in order, | 599 | Attempts to match @var{pattern1}@dots{}, in order, until one of them |
| 600 | until one of them fails to match. | 600 | fails to match. In that case, @code{and} likewise fails to match, and |
| 601 | In that case, @code{and} likewise fails to match, | 601 | the rest of the sub-patterns are not tested. If all sub-patterns |
| 602 | and the rest of the sub-patterns are not tested. | 602 | match, @code{and} matches. |
| 603 | If all sub-patterns match, @code{and} matches. | ||
| 604 | 603 | ||
| 605 | @item (or @var{pattern1} @var{pattern2}@dots{}) | 604 | @item (or @var{pattern1} @var{pattern2}@dots{}) |
| 606 | Attempts to match @var{pattern1}, @var{pattern2}, @dots{}, in order, | 605 | Attempts to match @var{pattern1}, @var{pattern2}, @dots{}, in order, |
| 607 | until one of them succeeds. | 606 | until one of them succeeds. In that case, @code{or} likewise matches, |
| 608 | In that case, @code{or} likewise matches, | 607 | and the rest of the sub-patterns are not tested. (Note that there |
| 609 | and the rest of the sub-patterns are not tested. | 608 | must be at least two sub-patterns. |
| 610 | (Note that there must be at least two sub-patterns. | ||
| 611 | Simply @w{@code{(or @var{pattern1})}} signals error.) | 609 | Simply @w{@code{(or @var{pattern1})}} signals error.) |
| 612 | @c Issue: Is this correct and intended? | 610 | @c Issue: Is this correct and intended? |
| 613 | @c Are there exceptions, qualifications? | 611 | @c Are there exceptions, qualifications? |
| @@ -1042,12 +1040,11 @@ Both use a single backquote construct (@pxref{Backquote}). | |||
| 1042 | 1040 | ||
| 1043 | This subsection describes @dfn{backquote-style patterns}, | 1041 | This subsection describes @dfn{backquote-style patterns}, |
| 1044 | a set of builtin patterns that eases structural matching. | 1042 | a set of builtin patterns that eases structural matching. |
| 1045 | For background, @xref{Pattern-Matching Conditional}. | 1043 | For background, @pxref{Pattern-Matching Conditional}. |
| 1046 | 1044 | ||
| 1047 | @dfn{Backquote-style patterns} are a powerful set of | 1045 | Backquote-style patterns are a powerful set of @code{pcase} pattern |
| 1048 | @code{pcase} pattern extensions (created using @code{pcase-defmacro}) | 1046 | extensions (created using @code{pcase-defmacro}) that make it easy to |
| 1049 | that make it easy to match @var{expval} against | 1047 | match @var{expval} against specifications of its @emph{structure}. |
| 1050 | specifications of its @emph{structure}. | ||
| 1051 | 1048 | ||
| 1052 | For example, to match @var{expval} that must be a list of two | 1049 | For example, to match @var{expval} that must be a list of two |
| 1053 | elements whose first element is a specific string and the second | 1050 | elements whose first element is a specific string and the second |
| @@ -1173,87 +1170,102 @@ evaluation results: | |||
| 1173 | (evaluate '(sub 1 2) nil) @result{} error | 1170 | (evaluate '(sub 1 2) nil) @result{} error |
| 1174 | @end example | 1171 | @end example |
| 1175 | 1172 | ||
| 1176 | @node Destructuring patterns | 1173 | @node Destructuring with pcase Patterns |
| 1177 | @subsection Destructuring Patterns | 1174 | @subsection Destructuring with @code{pcase} Patterns |
| 1178 | @cindex destructuring patterns | 1175 | @cindex destructuring with pcase patterns |
| 1179 | 1176 | ||
| 1180 | Pcase patterns not only express a condition on the form of the objects | 1177 | Pcase patterns not only express a condition on the form of the objects |
| 1181 | they can match but they can also extract sub-fields of those objects. | 1178 | they can match, but they can also extract sub-fields of those objects. |
| 1182 | Say we have a list and want to extract 2 elements from it with the | 1179 | For example we can extract 2 elements from a list that is the value of |
| 1183 | following code: | 1180 | the variable @code{my-list} with the following code: |
| 1184 | 1181 | ||
| 1185 | @example | 1182 | @example |
| 1186 | (pcase l | 1183 | (pcase my-list |
| 1187 | (`(add ,x ,y) (message "Contains %S and %S" x y))) | 1184 | (`(add ,x ,y) (message "Contains %S and %S" x y))) |
| 1188 | @end example | 1185 | @end example |
| 1189 | 1186 | ||
| 1190 | This will not only extract @code{x} and @code{y} but will additionally | 1187 | This will not only extract @code{x} and @code{y} but will additionally |
| 1191 | test that @code{l} is a list containing exactly 3 elements and whose | 1188 | test that @code{my-list} is a list containing exactly 3 elements and |
| 1192 | first element is the symbol @code{add}. If any of those tests fail, | 1189 | whose first element is the symbol @code{add}. If any of those tests |
| 1193 | @code{pcase} will directly return @code{nil} without calling | 1190 | fail, @code{pcase} will immediately return @code{nil} without calling |
| 1194 | @code{message}. | 1191 | @code{message}. |
| 1195 | 1192 | ||
| 1196 | @dfn{Destructuring} of an object is an operation that extracts | 1193 | Extraction of multiple values stored in an object is known as |
| 1197 | multiple values stored in the object, e.g., the 2nd and the 3rd | 1194 | @dfn{destructuring}. Using @code{pcase} patterns allows to perform |
| 1198 | element of a list or a vector. @dfn{Destructuring binding} is | 1195 | @dfn{destructuring binding}, which is similar to a local binding |
| 1199 | similar to a local binding (@pxref{Local Variables}), but it gives | 1196 | (@pxref{Local Variables}), but gives values to multiple elements of |
| 1200 | values to multiple elements of a variable by extracting those values | 1197 | a variable by extracting those values from an object of compatible |
| 1201 | from an object of compatible structure. | 1198 | structure. |
| 1202 | 1199 | ||
| 1203 | The macros described in this section use @dfn{destructuring | 1200 | The macros described in this section use @code{pcase} patterns to |
| 1204 | patterns}, which are normal Pcase patterns used in a context where we | 1201 | perform destructuring binding. The condition of the object to be of |
| 1205 | presume that the object does match the pattern, and we only want | 1202 | compatible structure means that the object must match the pattern, |
| 1206 | to extract some subfields. For example: | 1203 | because only then the object's subfields can be extracted. For |
| 1204 | example: | ||
| 1207 | 1205 | ||
| 1208 | @example | 1206 | @example |
| 1209 | (pcase-let ((`(add ,x ,y) l)) | 1207 | (pcase-let ((`(add ,x ,y) my-list)) |
| 1210 | (message "Contains %S and %S" x y)) | 1208 | (message "Contains %S and %S" x y)) |
| 1211 | @end example | 1209 | @end example |
| 1212 | 1210 | ||
| 1213 | @noindent | 1211 | @noindent |
| 1214 | does the same as the previous example, except that it directly tries | 1212 | does the same as the previous example, except that it directly tries |
| 1215 | to extract @code{x} and @code{y} from @code{l} without first verifying | 1213 | to extract @code{x} and @code{y} from @code{my-list} without first |
| 1216 | if @code{l} is a list which has the right number of elements and has | 1214 | verifying if @code{my-list} is a list which has the right number of |
| 1217 | @code{add} as its first element. | 1215 | elements and has @code{add} as its first element. The precise |
| 1218 | The precise behavior when the object does not actually match the | 1216 | behavior when the object does not actually match the pattern is |
| 1219 | pattern is undefined, although the body will not be silently skipped: | 1217 | undefined, although the body will not be silently skipped: either an |
| 1220 | either an error is signaled or the body is run with some of the | 1218 | error is signaled or the body is run with some of the variables |
| 1221 | variables potentially bound to arbitrary values like @code{nil}. | 1219 | potentially bound to arbitrary values like @code{nil}. |
| 1220 | |||
| 1221 | The pcase patterns that are useful for destructuring bindings are | ||
| 1222 | generally those described in @ref{Backquote Patterns}, since they | ||
| 1223 | express a specification of the structure of objects that will match. | ||
| 1224 | |||
| 1225 | For an alternative facility for destructuring binding, see | ||
| 1226 | @ref{seq-let}. | ||
| 1222 | 1227 | ||
| 1223 | @defmac pcase-let bindings body@dots{} | 1228 | @defmac pcase-let bindings body@dots{} |
| 1224 | Bind variables according to @var{bindings} and then eval @var{body}. | 1229 | Perform desctructuring binding of variables according to |
| 1230 | @var{bindings}, and then evaluate @var{body}. | ||
| 1225 | 1231 | ||
| 1226 | @var{bindings} is a list of bindings of the form @w{@code{(@var{pattern} | 1232 | @var{bindings} is a list of bindings of the form @w{@code{(@var{pattern} |
| 1227 | @var{exp})}}, where @var{exp} is an expression to evaluate and | 1233 | @var{exp})}}, where @var{exp} is an expression to evaluate and |
| 1228 | @var{pattern} is a destructuring pattern. | 1234 | @var{pattern} is a @code{pcase} pattern. |
| 1229 | 1235 | ||
| 1230 | All @var{exp}s are evaluated first after which they are matched | 1236 | All @var{exp}s are evaluated first, after which they are matched |
| 1231 | against their respective @var{pattern}, introducing new variable | 1237 | against their respective @var{pattern}, introducing new variable |
| 1232 | bindings which can then be used inside @var{body}. | 1238 | bindings that can then be used inside @var{body}. The variable |
| 1239 | bindings are produced by destructuring binding of elements of | ||
| 1240 | @var{pattern} to the values of the corresponding elements of the | ||
| 1241 | evaluated @var{exp}. | ||
| 1233 | @end defmac | 1242 | @end defmac |
| 1234 | 1243 | ||
| 1235 | @defmac pcase-let* bindings body@dots{} | 1244 | @defmac pcase-let* bindings body@dots{} |
| 1236 | Bind variables according to @var{bindings} and then eval @var{body}. | 1245 | Perform desctructuring binding of variables according to |
| 1246 | @var{bindings}, and then evaluate @var{body}. | ||
| 1237 | 1247 | ||
| 1238 | @var{bindings} is a list of bindings of the form @code{(@var{pattern} | 1248 | @var{bindings} is a list of bindings of the form @code{(@var{pattern} |
| 1239 | @var{exp})}, where @var{exp} is an expression to evaluate and | 1249 | @var{exp})}, where @var{exp} is an expression to evaluate and |
| 1240 | @var{pattern} is a destructuring pattern. | 1250 | @var{pattern} is a @code{pcase} pattern. The variable bindings are |
| 1241 | 1251 | produced by destructuring binding of elements of @var{pattern} to the | |
| 1242 | Unlike @code{pcase-let}, but like @code{let*}, each @var{exp} is | 1252 | values of the corresponding elements of the evaluated @var{exp}. |
| 1243 | matched against its corresponding @var{pattern} before passing to the | 1253 | |
| 1244 | next element of @var{bindings}, so the variables introduced in each | 1254 | Unlike @code{pcase-let}, but similarly to @code{let*}, each @var{exp} |
| 1245 | binding are available in the @var{exp}s that follow it, additionally | 1255 | is matched against its corresponding @var{pattern} before processing |
| 1246 | to being available in @var{body}. | 1256 | the next element of @var{bindings}, so the variable bindings |
| 1257 | introduced in each one of the @var{bindings} are available in the | ||
| 1258 | @var{exp}s of the @var{bindings} that follow it, additionally to | ||
| 1259 | being available in @var{body}. | ||
| 1247 | @end defmac | 1260 | @end defmac |
| 1248 | 1261 | ||
| 1249 | @findex dolist | ||
| 1250 | @defmac pcase-dolist (pattern list) body@dots{} | 1262 | @defmac pcase-dolist (pattern list) body@dots{} |
| 1251 | This construct executes @var{body} once for each element of | 1263 | Execute @var{body} once for each element of @var{list}, on each |
| 1252 | @var{list}, in a context where the variables appearing in the the | 1264 | iteration performing a destructuring binding of variables in |
| 1253 | destructuring pattern @var{pattern} are bound to the corresponding | 1265 | @var{pattern} to the values of the corresponding subfields of the |
| 1254 | values found in the element. | 1266 | element of @var{list}. The bindings are performed as if by |
| 1255 | When @var{pattern} is a simple variable, this ends up being equivalent | 1267 | @code{pcase-let}. When @var{pattern} is a simple variable, this ends |
| 1256 | to @code{dolist}. | 1268 | up being equivalent to @code{dolist} (@pxref{Iteration}). |
| 1257 | @end defmac | 1269 | @end defmac |
| 1258 | 1270 | ||
| 1259 | 1271 | ||
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 51d724cb1d8..60d017c3e44 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi | |||
| @@ -1049,15 +1049,18 @@ that @var{sequence} can be a list, vector or string. This is | |||
| 1049 | primarily useful for side-effects. | 1049 | primarily useful for side-effects. |
| 1050 | @end defmac | 1050 | @end defmac |
| 1051 | 1051 | ||
| 1052 | @defmac seq-let arguments sequence body@dots{} | 1052 | @anchor{seq-let} |
| 1053 | @defmac seq-let var-sequence val-sequence body@dots{} | ||
| 1053 | @cindex sequence destructuring | 1054 | @cindex sequence destructuring |
| 1054 | This macro binds the variables defined in @var{arguments} to the | 1055 | This macro binds the variables defined in @var{var-sequence} to the |
| 1055 | elements of @var{sequence}. @var{arguments} can themselves include | 1056 | values that are the corresponding elements of @var{val-sequence}. |
| 1056 | sequences, allowing for nested destructuring. | 1057 | This is known as @dfn{destructuring binding}. The elements of |
| 1058 | @var{var-sequence} can themselves include sequences, allowing for | ||
| 1059 | nested destructuring. | ||
| 1057 | 1060 | ||
| 1058 | The @var{arguments} sequence can also include the @code{&rest} marker | 1061 | The @var{var-sequence} sequence can also include the @code{&rest} |
| 1059 | followed by a variable name to be bound to the rest of | 1062 | marker followed by a variable name to be bound to the rest of |
| 1060 | @code{sequence}. | 1063 | @var{val-sequence}. |
| 1061 | 1064 | ||
| 1062 | @example | 1065 | @example |
| 1063 | @group | 1066 | @group |
| @@ -1081,6 +1084,9 @@ followed by a variable name to be bound to the rest of | |||
| 1081 | @end group | 1084 | @end group |
| 1082 | @result{} [3 4] | 1085 | @result{} [3 4] |
| 1083 | @end example | 1086 | @end example |
| 1087 | |||
| 1088 | The @code{pcase} patterns provide an alternative facility for | ||
| 1089 | destructuring binding, see @ref{Destructuring with pcase Patterns}. | ||
| 1084 | @end defmac | 1090 | @end defmac |
| 1085 | 1091 | ||
| 1086 | @defun seq-random-elt sequence | 1092 | @defun seq-random-elt sequence |
diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el index 2e89ae0779a..fde3bdb27f3 100644 --- a/lisp/emacs-lisp/pcase.el +++ b/lisp/emacs-lisp/pcase.el | |||
| @@ -264,10 +264,14 @@ variable name being but a special case of it)." | |||
| 264 | 264 | ||
| 265 | ;;;###autoload | 265 | ;;;###autoload |
| 266 | (defmacro pcase-let* (bindings &rest body) | 266 | (defmacro pcase-let* (bindings &rest body) |
| 267 | "Like `let*' but where you can use `pcase' patterns for bindings. | 267 | "Like `let*', but supports destructuring BINDINGS using `pcase' patterns. |
| 268 | BODY should be an expression, and BINDINGS should be a list of bindings | 268 | As with `pcase-let', BINDINGS are of the form (PATTERN EXP), but the |
| 269 | of the form (PATTERN EXP). | 269 | EXP in each binding in BINDINGS can use the results of the destructuring |
| 270 | See `pcase-let' for discussion of how PATTERN is matched." | 270 | bindings that precede it in BINDINGS' order. |
| 271 | |||
| 272 | Each EXP should match (i.e. be of compatible structure) to its | ||
| 273 | respective PATTERN; a mismatch may signal an error or may go | ||
| 274 | undetected, binding variables to arbitrary values, such as nil." | ||
| 271 | (declare (indent 1) | 275 | (declare (indent 1) |
| 272 | (debug ((&rest (pcase-PAT &optional form)) body))) | 276 | (debug ((&rest (pcase-PAT &optional form)) body))) |
| 273 | (let ((cached (gethash bindings pcase--memoize))) | 277 | (let ((cached (gethash bindings pcase--memoize))) |
| @@ -280,13 +284,16 @@ See `pcase-let' for discussion of how PATTERN is matched." | |||
| 280 | 284 | ||
| 281 | ;;;###autoload | 285 | ;;;###autoload |
| 282 | (defmacro pcase-let (bindings &rest body) | 286 | (defmacro pcase-let (bindings &rest body) |
| 283 | "Like `let' but where you can use `pcase' patterns for bindings. | 287 | "Like `let', but supports destructuring BINDINGS using `pcase' patterns. |
| 284 | BODY should be a list of expressions, and BINDINGS should be a list of bindings | 288 | BODY should be a list of expressions, and BINDINGS should be a list of |
| 285 | of the form (PATTERN EXP). | 289 | bindings of the form (PATTERN EXP). |
| 286 | The PATTERNs are only used to extract data, so the code does not test | 290 | All EXPs are evaluated first, and then used to perform destructuring |
| 287 | whether the data does match the corresponding patterns: a mismatch | 291 | bindings by matching each EXP against its respective PATTERN. Then |
| 288 | may signal an error or may go undetected, binding variables to arbitrary | 292 | BODY is evaluated with those bindings in effect. |
| 289 | values, such as nil." | 293 | |
| 294 | Each EXP should match (i.e. be of compatible structure) to its | ||
| 295 | respective PATTERN; a mismatch may signal an error or may go | ||
| 296 | undetected, binding variables to arbitrary values, such as nil." | ||
| 290 | (declare (indent 1) (debug pcase-let*)) | 297 | (declare (indent 1) (debug pcase-let*)) |
| 291 | (if (null (cdr bindings)) | 298 | (if (null (cdr bindings)) |
| 292 | `(pcase-let* ,bindings ,@body) | 299 | `(pcase-let* ,bindings ,@body) |
| @@ -304,11 +311,15 @@ values, such as nil." | |||
| 304 | 311 | ||
| 305 | ;;;###autoload | 312 | ;;;###autoload |
| 306 | (defmacro pcase-dolist (spec &rest body) | 313 | (defmacro pcase-dolist (spec &rest body) |
| 307 | "Superset of `dolist' where the VAR binding can be a `pcase' PATTERN. | 314 | "Eval BODY once for each set of bindings defined by PATTERN and LIST elements. |
| 308 | More specifically, this is just a shorthand for the following combination | 315 | PATTERN should be a `pcase' pattern describing the structure of |
| 309 | of `dolist' and `pcase-let': | 316 | LIST elements, and LIST is a list of objects that match PATTERN, |
| 310 | 317 | i.e. have a structure that is compatible with PATTERN. | |
| 311 | (dolist (x LIST) (pcase-let ((PATTERN x)) BODY...)) | 318 | For each element of LIST, this macro binds the variables in |
| 319 | PATTERN to the corresponding subfields of the LIST element, and | ||
| 320 | then evaluates BODY with these bindings in effect. The | ||
| 321 | destructuring bindings of variables in PATTERN to the subfields | ||
| 322 | of the elements of LIST is performed as if by `pcase-let'. | ||
| 312 | \n(fn (PATTERN LIST) BODY...)" | 323 | \n(fn (PATTERN LIST) BODY...)" |
| 313 | (declare (indent 1) (debug ((pcase-PAT form) body))) | 324 | (declare (indent 1) (debug ((pcase-PAT form) body))) |
| 314 | (if (pcase--trivial-upat-p (car spec)) | 325 | (if (pcase--trivial-upat-p (car spec)) |