Credence.Pattern.PreferCountsForLength
(credence v0.8.0)
Copy Markdown
Detects length(String.codepoints(string)) when a counts map from
Enum.frequencies/1 on the same codepoints already exists.
String.codepoints/1 traverses the string once for Enum.frequencies/1
and a second time for length/1. Derive n from the existing counts map
via Enum.sum(Map.values(counts)) to avoid the redundant traversal.
Bad
counts = string |> String.codepoints() |> Enum.frequencies()
n = length(String.codepoints(string))Good
counts = string |> String.codepoints() |> Enum.frequencies()
n = Enum.sum(Map.values(counts))