diff options
-rw-r--r-- | cppawk-cons.1 | 43 | ||||
-rw-r--r-- | testcases-cons | 24 |
2 files changed, 67 insertions, 0 deletions
diff --git a/cppawk-cons.1 b/cppawk-cons.1 index 2c11c4d..4b22827 100644 --- a/cppawk-cons.1 +++ b/cppawk-cons.1 @@ -2310,6 +2310,49 @@ is returned. list_end_atom(\fIbag\fP, 3) -> ("a" 1 2 . 3) .ft R +.SS Macro \fIbags\fP +.bk +Syntax: + +.ft B + bags (\fIb1\fP, \fIb2\fP, ...) \fIstatement\fP + bag (\fIbag\fP, \fIitem\fP) +.ft R + +The +.B bags +macro initializes one or more variables to empty bag values. +Then it executes a statement. After the statement, the bag +variables are converted to lists. + +Within the +.IR statement , +the +.B bag +helper macro is used for collecting items into the bags. +The expression +.BI bag( b ", " item ) +is a shorthand for +.IB b " = list_add(" b ", " item )\fR.\fP + +.B Example: + +.ft B + bags (\fIvals\fP, \fIsquares\fP, \fIsums\fP) { + \fIacc\fP = 0 + for (\fIi\fP = 0; \fIi\fP < 5; \fIi\fP++) { + bag (\fIvals\fP, \fIi\fP) + bag (\fIsquares\fP, \fIi\fP*\fIi\fP) + bag (\fIsums\fP, \fIacc\fP += \fIi\fP) + } + } + + \fI// the bags variables are now\fP + \fIvals\fP -> (0 1 2 3 4) + \fIsquares\fP -> (0 1 4 9 16) + \fIsums\fP -> (0 1 3 6 10) +.ft R + .SH "SEE ALSO" cppawk(1), cppawk-fun(1) diff --git a/testcases-cons b/testcases-cons index 76be7c8..e945478 100644 --- a/testcases-cons +++ b/testcases-cons @@ -849,3 +849,27 @@ nil 3 ("a" 1 2) ("a" 1 2 . 3) +-- +44: +$cppawk ' +#include <cons.h> + +BEGIN { + bags (vals, squares, sums) { + acc = 0 + for (i = 0; i < 5; i++) { + bag (vals, i) + bag (squares, i*i) + bag (sums, acc += i) + } + } + + // the bags variables are now + print sexp(vals) + print sexp(squares) + print sexp(sums) +}' +: +(0 1 2 3 4) +(0 1 4 9 16) +(0 1 3 6 10) |