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

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&amp;*())^% %%:&gt;" From this string, I want to only keep the integers, ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
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
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Other popular topics Top

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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement