Skip to content

Instantly share code, notes, and snippets.

@edubkendo
Created March 21, 2015 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edubkendo/2f8ce10d6008b03a809b to your computer and use it in GitHub Desktop.
Save edubkendo/2f8ce10d6008b03a809b to your computer and use it in GitHub Desktop.
defmodule MyApp do
def clean_string(str), do: String.codepoints(str) |> _clean([])
defp _clean([], result) do
result
|> Enum.reverse
|> to_string
end
defp _clean([h | t], result) do
case is_clean?(h) do
true ->
_clean(t, [h | result])
false ->
_clean(t, result)
end
end
defp is_clean?(str), do: String.match?(str, ~r/(\s|\w)/)
end
defmodule MyAppTest do
use ExUnit.Case
doctest MyApp
test "the truth" do
MyApp.clean_string("Th#is! i^s a& di@@rty sen$te#nc@e") == "This is a dirty sentence"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment