From 6634469ae805063cad62fef8ef0ab79fd8e10069132750ecffd715964e423ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Bj=C3=B6rnstam?= Date: Sun, 15 Nov 2026 13:23:06 +0100 Subject: [PATCH] someing in the accumulator table Co-Authored-By: Claude Opus 5.5 --- _01.Language-basics.sz | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/_01.Language-basics.sz b/_01.Language-basics.sz index 67d96bf..603f162 100644 --- a/_01.Language-basics.sz +++ b/_01.Language-basics.sz @@ -1588,6 +1588,7 @@ 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{(someing e)} | the first @code{(Some v)} the @code{Option} @code{e} gives, 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 | @@ -1599,17 +1600,18 @@ An accumulator names a collector and what to feed it: @code{folding} is the general one: inside @code{expr} the accumulator's own name is the value so far. -@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. +@code{finding}, @code{someing}, @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 +{someing (map-try-ref ages n) (:for n names)} ;; the age of the first name in ages, or 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 x)} returns @code{(someing (Some x))}, and @code{someing} returns a @code{folding} and a @code{(:finish ...)}. @codeblock[#:lang "bjolang"]|{ (def xs [3 9 2 7])