Skip to content

Instantly share code, notes, and snippets.

@adworse
Created September 13, 2020 00:35
Show Gist options
  • Save adworse/a7853bbf7bd67fea56647d0f5f906463 to your computer and use it in GitHub Desktop.
Save adworse/a7853bbf7bd67fea56647d0f5f906463 to your computer and use it in GitHub Desktop.
defmodule LessNaive do
def trim_string(string) do
string
|> String.graphemes()
|> Enum.reduce_while({0, ""}, fn grapheme, {count, acc} ->
count = count + counter(grapheme)
if count <= 80,
do: {:cont, {count, acc <> grapheme}},
else: {:halt, acc}
end)
end
defp counter(string), do: string |> String.codepoints() |> Enum.count()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment