Credence.Pattern.UnnecessaryGraphemeChunking (credence v0.7.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)//1 do
  String.slice(string, i, n)
end

String.length/1 and String.slice/3 both operate on grapheme clusters, so this is grapheme-equivalent. The //1 step keeps it equivalent even when the string is shorter than the chunk size: the range is then empty, matching Enum.chunk_every(_, n, 1, :discard) (a stepless 0..-k would descend and emit bogus slices).