This is a modified version of what I proposed in
https://github.com/elixir-lang/elixir/pull/11681Similar to `Map.pop/2` and and `Keyword.pop/2`, I propose adding `List.pop/2`.
I also propose adding `List.find_and_pop/2`, which is more generalized since it takes a function.
Here are possible implementations.
```elixir
@doc """
Returns and removes the first value matching `item` in the `list`.
## Examples
iex> List.pop([1, 2, 2, 3], 2)
{2, [1, 2, 3]}
iex> List.pop([1, 2, 3, 4], 20)
{nil, [1, 2, 3, 4]}
"""
def pop(list, item) do
find_and_pop(list, &(&1 == item))
end
@doc """
Returns and removes the first value matching `fun` in the `list`.
## Examples
iex> List.find_and_pop([1, 2, 3, 4], &(&1 > 2))
{3, [1, 2, 4]}
iex> List.find_and_pop([1, 2, 3, 4], &(&1 > 20))
{nil, [1, 2, 3, 4]}
"""
def find_and_pop(list, fun) do
case Enum.find_index(list, fun) do
nil -> {nil, list}
i -> pop_at(list, i)
end
end
```