Skip to content

Instantly share code, notes, and snippets.

@vysakh0
Created February 10, 2016 03:49
Show Gist options
  • Save vysakh0/2e0cdba0bd9de2e7e24a to your computer and use it in GitHub Desktop.
Save vysakh0/2e0cdba0bd9de2e7e24a to your computer and use it in GitHub Desktop.
Remove any given character in a string just using recursion
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
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