This is a modified version of what I proposed in
https://github.com/elixir-lang/elixir/pull/11681
Similar 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
```
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/bdc98b8c-fc73-40d7-8287-eb0b5d3ea97en%40googlegroups.com.