| 1 |
'\" |
| 2 |
'\" Copyright (c) 2012 Trevor Davel |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH lmap n "" Tcl "Tcl Built-In Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
lmap \- Iterate over all elements in one or more lists and collect results |
| 13 |
.SH SYNOPSIS |
| 14 |
\fBlmap \fIvarname list body\fR |
| 15 |
.br |
| 16 |
\fBlmap \fIvarlist1 list1\fR ?\fIvarlist2 list2 ...\fR? \fIbody\fR |
| 17 |
.BE |
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
The \fBlmap\fR command implements a loop where the loop variable(s) take on |
| 21 |
values from one or more lists, and the loop returns a list of results |
| 22 |
collected from each iteration. |
| 23 |
.PP |
| 24 |
In the simplest case there is one loop variable, \fIvarname\fR, and one list, |
| 25 |
\fIlist\fR, that is a list of values to assign to \fIvarname\fR. The |
| 26 |
\fIbody\fR argument is a Tcl script. For each element of \fIlist\fR (in order |
| 27 |
from first to last), \fBlmap\fR assigns the contents of the element to |
| 28 |
\fIvarname\fR as if the \fBlindex\fR command had been used to extract the |
| 29 |
element, then calls the Tcl interpreter to execute \fIbody\fR. If execution of |
| 30 |
the body completes normally then the result of the body is appended to an |
| 31 |
accumulator list. \fBlmap\fR returns the accumulator list. |
| 32 |
.PP |
| 33 |
In the general case there can be more than one value list (e.g., \fIlist1\fR |
| 34 |
and \fIlist2\fR), and each value list can be associated with a list of loop |
| 35 |
variables (e.g., \fIvarlist1\fR and \fIvarlist2\fR). During each iteration of |
| 36 |
the loop the variables of each \fIvarlist\fR are assigned consecutive values |
| 37 |
from the corresponding \fIlist\fR. Values in each \fIlist\fR are used in order |
| 38 |
from first to last, and each value is used exactly once. The total number of |
| 39 |
loop iterations is large enough to use up all the values from all the value |
| 40 |
lists. If a value list does not contain enough elements for each of its loop |
| 41 |
variables in each iteration, empty values are used for the missing elements. |
| 42 |
.PP |
| 43 |
The \fBbreak\fR and \fBcontinue\fR statements may be invoked inside |
| 44 |
\fIbody\fR, with the same effect as in the \fBfor\fR and \fBforeach\fR |
| 45 |
commands. In these cases the body does not complete normally and the result is |
| 46 |
not appended to the accumulator list. |
| 47 |
.SH EXAMPLES |
| 48 |
.PP |
| 49 |
Zip lists together: |
| 50 |
.PP |
| 51 |
.CS |
| 52 |
set list1 {a b c d} |
| 53 |
set list2 {1 2 3 4} |
| 54 |
set zipped [\fBlmap\fR a $list1 b $list2 {list $a $b}] |
| 55 |
# The value of zipped is "{a 1} {b 2} {c 3} {d 4}" |
| 56 |
.CE |
| 57 |
.PP |
| 58 |
Filter a list to remove odd values: |
| 59 |
.PP |
| 60 |
.CS |
| 61 |
set values {1 2 3 4 5 6 7 8} |
| 62 |
proc isEven {n} {expr {($n % 2) == 0}} |
| 63 |
set goodOnes [\fBlmap\fR x $values {expr { |
| 64 |
[isEven $x] ? $x : [continue] |
| 65 |
}}] |
| 66 |
# The value of goodOnes is "2 4 6 8" |
| 67 |
.CE |
| 68 |
.PP |
| 69 |
Take a prefix from a list based on the contents of the list: |
| 70 |
.PP |
| 71 |
.CS |
| 72 |
set values {8 7 6 5 4 3 2 1} |
| 73 |
proc isGood {counter} {expr {$n > 3}} |
| 74 |
set prefix [\fBlmap\fR x $values {expr { |
| 75 |
[isGood $x] ? $x : [break] |
| 76 |
}}] |
| 77 |
# The value of prefix is "8 7 6 5 4" |
| 78 |
.CE |
| 79 |
.SH "SEE ALSO" |
| 80 |
break(n), continue(n), for(n), foreach(n), while(n) |
| 81 |
.SH KEYWORDS |
| 82 |
foreach, iteration, list, loop, map |
| 83 |
'\" Local Variables: |
| 84 |
'\" mode: nroff |
| 85 |
'\" End: |