Why is DateTime module lacking min and max functions?

I am wondering why there is no min and max functions in Elixir for DateTime. Currently, you need to write something like :

if DateTime.compare(d1, d2) == :lt, do: d1, else: d2

Is there any reason why there is no DateTime.min(d1,d2) ?

1 Like

Fun fact, you can do this with Enum!

Enum.min([d1, d2], DateTime)
18 Likes

Very cool! Thanks for sharing @benwilson512 ! I’m looking at the docs for Date and I’m not sure about:

  • where to include this tidbit (super useful, even more for new folks coming into the language)
  • if we should include it in docs at all?

This is where I was looking by the way: elixir/date.ex at main · elixir-lang/elixir · GitHub

This was a TIL for me, usually date comparison was handled by a library in a few of the projects I’ve worked on in the past. This is great to know.

1 Like

Very interesting !

The introduction of the DateTime module says

It could be a good place to add information about the fact that the existence of the compare\2 function allows to use Enum.min\2

4 Likes

Agreed, I think that’d be exactly the right spot.

Cool, I made a PR :slight_smile:

5 Likes