[Proposal] Enum.max/2, Enum.min/2, Enum.min_max/2

87 views
Skip to first unread message

Myron Marston

unread,
Aug 11, 2016, 1:59:10 PM8/11/16
to elixir-lang-core

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)
  end
end

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

José Valim

unread,
Aug 11, 2016, 2:25:28 PM8/11/16
to elixir-l...@googlegroups.com
I am +1 for the function based function because we can argue that the default function raises Enum.EmptyError. So we keep backwards compatibility and get a feature out of it. :)



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
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 elixir-lang-core+unsubscribe@googlegroups.com.
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.

José Valim

unread,
Aug 13, 2016, 2:59:33 PM8/13/16
to elixir-l...@googlegroups.com
Btw, if you don't have time to send a PR, please open up an issue and someone will certainly grab it. :)



José Valim
Skype: jv.ptec
Founder and Director of R&D

Myron Marston

unread,
Aug 13, 2016, 6:33:11 PM8/13/16
to elixir-lang-core, jose....@plataformatec.com.br
Thanks, José.  I plan to open a PR soon.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages