Dispatching a list

Hello, have a list of tuples following the convention {:ok, “something”} {:error, "something else}. I’d like to dispatch the tuples in 2 different variables, using Enum.into to remove the :ok/:error “header”, I then call Enum.into over the same list matching on :ok and :error.
It doesn’t work, when a list doesn’t have any :error for example it throws a (FunctionClauseError) no function clause matching

elements = Enum.map(to_process, fn(target) -> adapter.get(target) end)
doing = Enum.into(elements, [], fn({:ok, element}) -> element end)
errors = Enum.into(elements, [], fn({:error, element}) -> element end)

How can I accomplish that?

ngw

There is already a function in the Enum module for doing that. :slight_smile:

Since you did not give an example dataset for me to run it over, this is untested, but:

elements = Enum.map(to_process, fn(target) -> adapter.get(target) end)
{doing, errors} = Enum.split_with(elements, &(elem(&1,0)==:ok))