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

AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14403 124
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&amp;*())^% %%:&gt;" From this string, I want to only keep the integers, ...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement