53. Folds and sorts of a sequence are methods (2026-11-21)
- Method form is canonical for a fold or sort of one sequence:
xs.sum(),xs.prod(),xs.min(),xs.max(),xs.count(p),xs.all(p),xs.any(p),xs.first(p),xs.sorted(by: k),xs.sort(by: k). The free form stays only where it reads as math: a generator (sum(f(x) for x in xs),count(c == x for c in cs)) or two or more values (min(a, b)). EveryList,[D]T,Range,SetandStrhas the methods; each lowers to the same fold as the free form, so the interpreter, C++,xs.max()!/?? dand the costs are identical. Reason: ports mixedcount(xs, p),sort(mut xs, by:)andxs.all(p)in one file; a receiver reads left to right, chains (xs[1..].max()), and completes in the editor. Cost: one token per site (a.sum()is 5 tokens,sum(a)4), which moved two dream gaps by under 0.5%. Reverse: deletefree_fold(L0509) and run the inverse rewrite; the free forms never stopped working. - The element a test holds for is
first,findis the index of a value.xs.find(x => ..)(batch 14) andfirst(xs, p)(§26.9) said the same thing;xs.firstis already the first element, soxs.first(p)is the first one a test holds for, asxs.count(p)besidexs.count(x). L0509 rewritesxs.find(lambda)toxs.first(lambda). Reverse: drop that arm offree_fold. cpl lint --fix(L0509) is the codemod. It skips receivers that are types or domains (sorted(V, by: k)walks a domain) andmin([a, b, c]), which waits on a variadicmin(a, b, c)from the core lane. Dreams that do not parse yet were migrated with the same patterns by hand.