Credence.Rule.UnnecessaryGraphemeChunking (credence v0.3.1)

Copy Markdown

Detects the common n-gram generation pipeline that converts a string to graphemes, creates sliding window chunks of step 1, and joins them back into strings. This pattern is automatically fixed by replacing it with String.slice/3 which avoids intermediate list allocations.

Bad

string
|> String.graphemes()
|> Enum.chunk_every(n, 1, :discard)
|> Enum.map(&Enum.join/1)

Good

for i <- 0..(String.length(string) - n) do
  String.slice(string, i, n)
end

String.length/1 and String.slice/3 both operate on grapheme clusters, so this replacement is semantically equivalent for the common case where the string length >= chunk size.