From 7d1cc99efe8288de8570d15f72619d54ffd98b64253e1cf9b4573997d4e20fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Bj=C3=B6rnstam?= Date: Sun, 15 Nov 2026 12:46:25 +0100 Subject: [PATCH] The new collectors in the accumulator table Co-Authored-By: Claude Opus 5.5 --- _01.Language-basics.sz | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/_01.Language-basics.sz b/_01.Language-basics.sz index 987da21..67d96bf 100644 --- a/_01.Language-basics.sz +++ b/_01.Language-basics.sz @@ -1588,15 +1588,25 @@ An accumulator names a collector and what to feed 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{(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{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"]|{ {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 +{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 ...)}.