I have a situation where I want to reduce a list of items like:
thing = [1, 2, 3, 4]
Enum.reduce(thing, [], fn el, acc ->
case el do
1 ->
["one" | acc]
2 ->
["two" | acc]
4 ->
["four" | acc]
end
end)
of course this is going to raise an exception when it gets to 3 because there is no case for that. I basically want to raise the exception, but also not crash the process and essentially just skip over the problem element in the reduce function. I was thinking of doing something like:
thing = [1, 2, 3, 4]
Enum.reduce(thing, [], fn el, acc ->
case el do
1 ->
["one" | acc]
2 ->
["two" | acc]
4 ->
["four" | acc]
_ ->
Task.start(fn -> raise ArgumentError, "Case missing for #{el}" end)
acc
end
but was wondering if there was a better approach?