Runs counted by hand
if c == x: run += 1 else: run = 0: s.runs() yields (val, len)
let s: Str = read()
var total = 0
var run = 0
for c in s:
if c == 'f':
run += 1
else:
total += run / 2
run = 0
total += run / 2
print(total)let s: Str = read()
print(sum(r.len / 2 for r in s.runs() if r[0] == 'f'))Why
A counter that grows on matching elements and resets to 0 on the others is a run-length scan, and it needs a second copy of its body after the loop for the last run, which is the line newcomers forget. xs.runs() from std.seq yields every run as (val, len), the last one included, so the work happens once per run. There is no automatic fix, since what is done with each run varies.
The practice it serves
A hand-written pattern the language or the standard library already says in one step.
Not flagged
A near miss: close to the pattern, but not it.
let s: Str = read()
var total = 0
var run = 0
for c in s:
if c == 'f':
run += 1
else:
total += run
print(total)Turn it off
For one block, wrap it in with lint(runs_by_hand: allow):. For a project, add runs_by_hand = "allow" under [lints] in cp.toml.
Generated from spec/lints/runs_by_hand, the same record the linter is built from.