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

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
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
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
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -&gt; H; nth(N, [_|T]) when N &gt; 1 -&gt; nth(N - 1, T). Elixir Enum.at … coo...
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
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement