Created
March 21, 2015 06:37
-
-
Save edubkendo/2f8ce10d6008b03a809b to your computer and use it in GitHub Desktop.
This file contains 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 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