finding: a collector that ends the loop

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

View file

@ -1587,10 +1587,20 @@ An accumulator names a collector and what to feed it:
| @code{(summing x)} | the sum of @code{x} |
| @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{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.
@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
}|
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 ...)}.
@codeblock[#:lang "bjolang"]|{
(def xs [3 9 2 7])