nunobernardes99

nunobernardes99

Find maximum and minimum in two dates

Hi there Elixir friends :vulcan_salute:

In a recent task I was on, I needed to check in two dates which of them is the maximum and which of them was the minimum. I’ve found out that using max and min functions from Kernel would work. Per example:

max(~D[2020-12-18], ~D[2020-12-10]) 
iex> ~D[2020-12-18]

min(~D[2020-12-18], ~D[2020-12-10]) 
iex> ~D[2020-12-10]

Using this solved the problem in a few seconds. When running the tests in my app, I’ve noticed that in some edge cases one or maybe both of the Dates could be nil values, but even with that, no error was being given in max and min. Started to dig out both functions, and I’ve found this.
With the max function everything was being returned as I was expecting when a nil value was encountered, it would return the other value and it would only return nil if both values were nil. This behaviour was correct.

iex> max(~D[2020-12-18], nil)
~D[2020-12-18]
iex> max(~D[2020-12-18], ~D[2020-12-10])
~D[2020-12-18]
iex> max(~D[2020-12-18], nil)           
~D[2020-12-18]
iex> max(nil, ~D[2020-12-10])           
~D[2020-12-10]
iex> max(nil, nil)           
nil

But, when testing the min function, the behaviour was not equal to what max was having.

iex> min(~D[2020-12-18], ~D[2020-12-10])
~D[2020-12-10]
iex> min(~D[2020-12-18], nil)           
nil
iex> min(nil, ~D[2020-12-10])           
nil
iex> min(nil, nil)           
nil

As you can see, when the function receives any parameter that it is nil, simply returns the nil, expecting it to be the minimum between both values, even if one of them is a Date type. This, in my opinion, is not correct behaviour. With this doubt, I talked with a co-worker and he pointed out it could be because of nil being read as Epoch 0. Is theory was probably correct, but today I tried this.

iex> min(~D[1970-01-01], nil)
nil
iex> min(~D[1969-01-01], nil)
nil

With this, I just simply removed the possibility of nil being considered as epoch 0, and now I’m wondering if it makes sense to contribute to Elixir Repo with a possible solution to this. Does anyone already think about this and what was their conclusions and does it make sense the behaviour of min? (or even max just straight return the same as what min returns).

Best regards!

Most Liked

LostKobrakai

LostKobrakai

That doesn’t properly compare dates:

iex(3)> max(~D[2020-12-18], ~D[2020-08-21])
~D[2020-08-21]

For comparing dates you should either use Date.compare or Enum.sort(list, Date).

ityonemo

ityonemo

iex(10)> nil < DateTime.utc_now()
true
iex(11)> nil > DateTime.utc_now()
false

Does this help?

It’s correct behaviour. Elixir and Erlang impose a total ordering over ALL terms. You can compare any two terms for a stable Enum.sort. This can sometimes lead to unexpected effects when the total ordering is not topical.

If you need topical comparisons, use the compare behaviour. Specifically for DateTime, there is DateTime.compare/2, and you can use Enum.sort/2 with the module that implements compare as the second parameter.

ityonemo

ityonemo

the admonition against using max/min for dates is in the documentation:

https://hexdocs.pm/elixir/master/Kernel.html#max/2

Where Next?

Popular in Discussions Top

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |&gt; https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New

We're in Beta

About us Mission Statement