You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+12-1
Original file line number
Diff line number
Diff line change
@@ -133,12 +133,13 @@ The simple form is `(accumulator operatior &optional initially)`.
133
133
-`operator` is the operator corresponding to the accumulator: this denotes a function which can take either two or no arguments: it is called with no arguments to initialise the variable underlying the accumulator if there is no `initially` value (this is the only case it is called with no arguments), or with the current value of the accumulator and the thing to be accumulated when the local function is called to accumulate something.
134
134
-`initially`, if given, is the initial value. If it is not given the accumulator is initialised by calling the operator function with no arguments.
135
135
136
-
The more general form is `(accumulator operator &key initially type returner)`.
136
+
The more general form is `(accumulator operator &key initially type returner default by)`.
137
137
138
138
-`accumulator`, `operator` and `initially` are the same as before.
139
139
-`type` is a type specification which is used to declare the type of the underlying variable.
140
140
-`returner` denotes a function of one argument which, if given, is called with the final value of the accumulator: its return value is used instead of the value of the accumulator.
141
141
-`default`, if given, causes the accumulator function to have a single optional argument for which this expression provides the default value.
142
+
-`by`, if given, means that the accumulator takes no arguments and steps the value by `by`. `by` and `default` can't both be given.
142
143
- There may be additional keyword arguments in future.
143
144
144
145
An example: let's say you want to walk some cons tree counting symbols:
@@ -196,6 +197,16 @@ There is no single-accumulator special case: it didn't seem useful as you need t
196
197
197
198
The accumulation operator and returner are *names* which denote functions, not functions: they can be either symbols or a lambda expressions, they can't be functions. Specifically it needs to be the case that `(operator ...)` is a valid form. That means that if you do want to use some function you'd need to provide an operator which was, for instance `(lambda (into val) (funcall f into val))`.
198
199
200
+
Both `by` and `default` are forms which are evaluated at call time in the current lexical and dynamic environment (they're simply the defaults for arguments). So this will return `3`:
201
+
202
+
```lisp
203
+
(let ((by 1))
204
+
(with-accumulators ((a + :by by))
205
+
(a)
206
+
(setf by 2)
207
+
(a)))
208
+
```
209
+
199
210
`with-accumulators` is very much newer than either of the other two macros, and may be more buggy. It is certainly the case that new keywords may appear in accumulator specifications.
200
211
201
212
`with-accumulators` can implement `collecting` or `with-collectors`:
0 commit comments