Credence.Pattern.PreferEnumSplit
(credence v0.7.0)
Copy Markdown
Performance rule: collapses two adjacent assignments that take and drop the
same prefix of the same list variable into a single Enum.split/2, which
produces both results in one pass.
Bad
first = Enum.take(items, 3)
rest = Enum.drop(items, 3)Good
{first, rest} = Enum.split(items, 3)Scope (the safe core)
Fires only when ALL of these hold for two adjacent statements in a block:
a = Enum.take(src, n)immediately followed byb = Enum.drop(src, n).srcis the same simple variable in both calls.nis the same non-negative integer literal in both calls.aandbare simple variable names witha != b.ais not the source variablesrc(otherwise thetakerebinds the list thedropthen reads — a different value).
Does NOT fire (deliberately dropped — not provably behaviour-preserving)
- Negative or variable counts.
Enum.split(l, n)only equals{Enum.take(l, n), Enum.drop(l, n)}for non-negativen; for negativenthe two halves swap. A variable count's sign is unknown, so it is skipped. Enum.reverse(Enum.drop(...))— would need a freshrestbinding and a second statement.- Piped forms (
src |> Enum.take(n)). - Non-adjacent take/drop (statements between them may rebind
src). takerebindingsrc(src = Enum.take(src, n)).takeordropalone, different sources, different counts, unbound calls.