The new collectors in the accumulator table

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-15 12:46:25 +01:00
parent dc629fbd3e
commit 7d1cc99efe

View file

@ -1588,15 +1588,25 @@ An accumulator names a collector and what to feed it:
| @code{(counting x)} | how many rounds reached it | | @code{(counting x)} | how many rounds reached it |
| @code{(folding init expr)} | starts at @code{init}; each round, @code{expr} is the new value | | @code{(folding init expr)} | starts at @code{init}; each round, @code{expr} is the new value |
| @code{(finding x)} | @code{(Some x)} for the first round that reaches it, which ends the loop, or @code{None} | | @code{(finding x)} | @code{(Some x)} for the first round that reaches it, which ends the loop, or @code{None} |
| @code{(anding test)} | whether @code{test} holds in every round; the first that fails ends the loop |
| @code{(oring test)} | whether @code{test} holds in some round; the first that holds ends the loop |
| @code{(taking n x)} | a @code{List} of the first @code{n}; the @code{n}th ends the loop |
| @code{(maximizing x)} | @code{(Some x)} of the greatest, by @code{Ord}, or @code{None} (and @code{minimizing}) |
| @code{(maximizing-by k x)} | @code{(Some x)} of the greatest key @code{k}, or @code{None} (and @code{minimizing-by}) |
| @code{(tallying x)} | a @code{Map} from each value to how many times it came |
| @code{(grouping k x)} | a @code{Map} from each key @code{k} to the @code{List} of its values |
} }
@code{folding} is the general one: inside @code{expr} the accumulator's own name is the value so far. @code{folding} is the general one: inside @code{expr} the accumulator's own name is the value so far.
@code{finding} stops the loop: the round that finds is the last, and the clauses after it in that round do not run. With @code{#:when}, or a loose @code{:when} in a comprehension, it finds the first value the condition holds for. @code{finding}, @code{anding}, @code{oring} and @code{taking} stop the loop: the round that decides is the last, and the clauses after it in that round do not run. With @code{#:when}, or a loose @code{:when} in a comprehension, it finds the first value the condition holds for.
@codeblock[#:lang "bjolang"]|{ @codeblock[#:lang "bjolang"]|{
{finding x (:for x [3 5 8 9]) (:when (even? x))} ;; (Some 8) {finding x (:for x [3 5 8 9]) (:when (even? x))} ;; (Some 8)
{finding x :when (> x 100) (:for x [3 5 8 9])} ;; None {finding x :when (> x 100) (:for x [3 5 8 9])} ;; None
{anding (odd? x) (:for x [3 5 8 9])} ;; #f, after the round of 8
{maximizing-by (string-length w) w (:for w (list "ab" "cde" "f"))} ;; (Some "cde")
{grouping (% x 2) x (:for x [3 5 8 9])} ;; a map from 0 to (8) and 1 to (3 5 9)
}| }|
It is a collector macro, written with @code{def/acc-clause}: a macro at the head of an accumulator's collector, which returns loop clauses that go where the @code{(:acc ...)} was. @code{finding} returns a @code{folding} and a @code{(:finish ...)}. It is a collector macro, written with @code{def/acc-clause}: a macro at the head of an accumulator's collector, which returns loop clauses that go where the @code{(:acc ...)} was. @code{finding} returns a @code{folding} and a @code{(:finish ...)}.