Created
September 13, 2020 00:35
-
-
Save adworse/a7853bbf7bd67fea56647d0f5f906463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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