diff --git a/solutions/elixir/strain/1/lib/strain.ex b/solutions/elixir/strain/1/lib/strain.ex new file mode 100644 index 0000000..1b7c4a3 --- /dev/null +++ b/solutions/elixir/strain/1/lib/strain.ex @@ -0,0 +1,27 @@ +defmodule Strain do + @doc """ + Given a `list` of items and a function `fun`, return the list of items where + `fun` returns true. + + Do not use `Enum.filter`. + """ + @spec keep(list :: list(any), fun :: (any -> boolean)) :: list(any) + def keep(list, fun) do + list + |> Enum.reduce([], &if(fun.(&1), do: [&1 | &2], else: &2)) + |> Enum.reverse() + end + + @doc """ + Given a `list` of items and a function `fun`, return the list of items where + `fun` returns false. + + Do not use `Enum.reject`. + """ + @spec discard(list :: list(any), fun :: (any -> boolean)) :: list(any) + def discard(list, fun) do + list + |> Enum.reduce([], &if(fun.(&1), do: &2, else: [&1 | &2])) + |> Enum.reverse() + end +end