← All lints

free_fold

L0509Idiomswarn

Fold or sort of a sequence written as a free function

sum(xs), count(xs, p) or sort(mut xs): the method reads on the sequence

Flagged
let xs = [3, 1, 4]
print(sum(xs), count(xs, x => x > 2))
Write instead
let xs = [3, 1, 4]
print(xs.sum(), xs.count(x => x > 2))

Why

A fold or sort of one sequence is a method on it: xs.sum(), xs.count(p), xs.all(p), xs.find(p), xs.max(), xs.sorted(by: k), xs.sort(). The free form stays where it reads as math: a generator (sum(f(x) for x in xs)) or two or more values (min(a, b)). cpl lint --fix rewrites it.

The practice it serves

A hand-written pattern the language or the standard library already says in one step. Family: S.seq: reductions.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
let xs = [3, 1, 4]
print(xs.sum(), xs.count(x => x > 2))

Turn it off

For one block, wrap it in with lint(free_fold: allow):. For a project, add free_fold = "allow" under [lints] in cp.toml.

Generated from spec/lints/free_fold, the same record the linter is built from.