From e16fe540e1e423b6b13ec41b91eb491dda50f066 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 19:28:19 +0000 Subject: [PATCH] elixir/strain/1 --- solutions/elixir/strain/1/lib/strain.ex | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 solutions/elixir/strain/1/lib/strain.ex 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