When using Enum’s min/max/min_max functions, I frequently have to provide
special case logic to deal with an empty collection, such as this:
def date_range_from(all_time_slices) do
case all_time_slices do
[] -> {nil, nil}
[_ | _] -> Enum.min_max(all_time_slices)
endend
It would be nice if there was a 2-arity version of these functions that you
could pass to determine what to do with an empty collection. For this case,
I’m imagining:
def date_range_from(all_time_slices) do
Enum.min_max(all_time_slices, {nil, nil})end
This aligns with functions like Map.get/3 that allow you to pass a default.
Alternately, I could see an argument for requiring the 2nd argument to be a
function:
def date_range_from(all_time_slices) do
Enum.min_max(all_time_slices, fn -> {nil, nil} end)end
Then the 1-arity version of these functions could just call the 2-arity
version with a function that raises an Enum.EmptyError. That said, I think
I prefer the version that allows you to pass a default return value w/ no
need to wrap it in a function.
Thoughts?
Myron
--
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/CADUxQmvSuQrudQ07U9Tvbwek-KRL_c9r9eW_P-RQ%2BbGM07do4A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.