28 lines
662 B
Elixir
28 lines
662 B
Elixir
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
|
|
for item <- list, fun.(item), into: [] do
|
|
item
|
|
end
|
|
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
|
|
for item <- list, not fun.(item), into: [] do
|
|
item
|
|
end
|
|
end
|
|
end
|