Created
February 10, 2016 03:49
-
-
Save vysakh0/2e0cdba0bd9de2e7e24a to your computer and use it in GitHub Desktop.
Remove any given character in a string just using recursion
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 Any do | |
def remove(str, char) do | |
remove("", str, char) | |
end | |
def remove(newstr, << char :: binary-size(1), rest :: binary >>, char) do | |
remove(newstr, rest, char) | |
end | |
def remove(newstr, << first :: binary-size(1), rest :: binary >>, char) do | |
remove(newstr <> first, rest, char) | |
end | |
def remove(newstr, <<>>, _char), do: newstr | |
end |
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 AnyTest do | |
use ExUnit.Case | |
test "remove any given characters from a string" do | |
assert Any.remove("Elixir", "x") === "Eliir" | |
assert Any.remove("Elixir", "E") === "lixir" | |
assert Any.remove(" E l i x i r", " ") === "Elixir" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment