Sha256: f32bb5ff72e7eb9032b00936b64b5fdff5adb16c006845647c925d486683339c

Contents?: true

Size: 1.05 KB

Versions: 69

Compression:

Stored size: 1.05 KB

Contents

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
    do_keep(list, fun, [])
  end

  defp do_keep([], _, results), do: Enum.reverse(results)

  defp do_keep([head | tail], fun, results) do
    case apply(fun, [head]) do
      true -> do_keep(tail, fun, [head | results])
      _ -> do_keep(tail, fun, results)
    end
  end

  @doc """
  Given a `list` of items and a function `fun`, return the list of items where
  `fun` returns true.

  Do not use `Enum.reject`.
  """
  @spec discard(list :: list(any), fun :: (any -> boolean)) :: list(any)
  def discard(list, fun) do
    do_discard(list, fun, [])
  end

  defp do_discard([], _, results), do: Enum.reverse(results)

  defp do_discard([head | tail], fun, results) do
    case apply(fun, [head]) do
      true -> do_discard(tail, fun, results)
      _ -> do_discard(tail, fun, [head | results])
    end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/strain/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/strain/example.exs