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 by b = Enum.drop(src, n).
  • src is the same simple variable in both calls.
  • n is the same non-negative integer literal in both calls.
  • a and b are simple variable names with a != b.
  • a is not the source variable src (otherwise the take rebinds the list the drop then 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-negative n; for negative n the two halves swap. A variable count's sign is unknown, so it is skipped.
  • Enum.reverse(Enum.drop(...)) — would need a fresh rest binding and a second statement.
  • Piped forms (src |> Enum.take(n)).
  • Non-adjacent take/drop (statements between them may rebind src).
  • take rebinding src (src = Enum.take(src, n)).
  • take or drop alone, different sources, different counts, unbound calls.