Skip to content

Instantly share code, notes, and snippets.

@iboard
Created December 2, 2021 09:09
Show Gist options
  • Save iboard/9d70aee99816610bc199d12941c4bf1b to your computer and use it in GitHub Desktop.
Save iboard/9d70aee99816610bc199d12941c4bf1b to your computer and use it in GitHub Desktop.
defmodule Strings do
# def remove_chars!(str, _chars) do
# String.replace(str, ~r([\-\+\/\*]), "")
# end
def remove_chars(str, chars) do
source = String.split(str, "")
chars = String.split(chars, "")
Enum.reduce(source, [], fn ch, acc ->
if ch not in chars do
acc ++ [ch]
else
acc
end
end)
|> Enum.join()
end
def split_even_and_odd(numbers) do
Enum.reduce(numbers, %{odd: [], even: []}, fn n, splitted ->
if rem(n, 2) == 0 do
%{splitted | even: splitted[:even] ++ [n]}
else
%{splitted | odd: splitted[:odd] ++ [n]}
end
end)
end
def collect_odd_numbers(numbers) do
Enum.reduce(numbers, [], fn n, odds ->
if rem(n, 2) == 0, do: odds, else: odds ++ [n]
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment