Skip to content

Instantly share code, notes, and snippets.

View adworse's full-sized avatar

Dima Ermilov adworse

  • Apptopia
  • Varna, Bulgaria
View GitHub Profile
defmodule StringTrimmer do
def trim_utf8(string, len), do: trim(string, len, &utf8_counter/1)
def trim_utf16(string, len), do: trim(string, len, &utf16_counter/1)
def trim(string, len, counter) do
string
|> String.graphemes()
|> Enum.reduce_while({0, ""}, fn grapheme, {count, acc} ->
count = count + counter.(grapheme)