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)
?
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)
?
Fun fact, you can do this with Enum!
Enum.min([d1, d2], DateTime)
Very cool! Thanks for sharing @benwilson512 ! I’m looking at the docs for Date
and I’m not sure about:
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.
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
Agreed, I think that’d be exactly the right spot.
Cool, I made a PR