add subtract functions in date-related modules to increase readability?

32 views
Skip to first unread message

Thomas J.

unread,
Jan 30, 2020, 6:42:15 AM1/30/20
to elixir-lang-core
Hello,

I'm a little surprised for having to use a function named `add` (with a given negative value) to subtract days.

Why not add subtract functions to make the code easier to read?

Andrea Leopardi

unread,
Jan 31, 2020, 10:45:05 AM1/31/20
to elixir-lang-core
Hey Thomas,

we usually try to strike a good balance between having as little functions as possible in the standard library (to avoid bloating it) and having enough functions so that your code can be efficient and expressive. In this case, the performance is not different, so adding it would only be a readability concern. Personally, I feel like using "add/2" and negating the second argument is clear enough that it doesn't warrant an additional function to the date-related modules.

Cheers,

Andrea Leopardi


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/61dd1db0-925c-4cf0-9cd9-5bfa339a9478%40googlegroups.com.

Manfred Bergmann

unread,
Jan 31, 2020, 11:15:02 AM1/31/20
to elixir-l...@googlegroups.com
Hi.

I believe in math it’s completely legit to write: 1 + (-1) = 0


Manfred
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAM9Rf%2BKg2P1HQUybE%2B%3DLbiBe%2BjCYp0HKsxr%2BOx9N%3D2Tqeg2RwA%40mail.gmail.com.

Xavier Noria

unread,
Jan 31, 2020, 1:49:39 PM1/31/20
to elixir-l...@googlegroups.com
On Fri, Jan 31, 2020 at 5:14 PM 'Manfred Bergmann' via elixir-lang-core <elixir-l...@googlegroups.com> wrote:
Hi.

I believe in math it’s completely legit to write: 1 + (-1) = 0

Yes. Even more, technically the canonical algebraic structure for integers (a ring called Z) has addition and multiplication only. There is a concept of "the opposite of an integer": the opposite of N is a number M such that N + M = 0. By convention, you write "-N" as an alias for said (unique) M, and X - Y is a shortcut for X + (-Y). There is no subtraction operation.

So, your notation is actually quite literally exact.


Reply all
Reply to author
Forward
0 new messages