I am definitely in favor of clearer APIs.However, it would probably be best to explore how different libraries in different languages tackle this. Can you please explore this? In particular, I am curious to know if before/after mean "<" and ">" respectively or if they mean "<=" and "=>" (I assume the former). And also if some libraries feel compelled to expose functions such as "after_or_equal" or if users would have to write Date.equal?(date1, date2) or Date.earlier?(date1, date2), which would end-up doing the double of conversions.
--
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/CAGnRm4%2B3oDVk5hpF7EpfZt4Xa0k7v1Rk%2BxFF9eGtWCXmO4JfpA%40mail.gmail.com.
--
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/fcd07389-c6a0-497d-9c09-7f1eacf620c6n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/e6c55604-c3ea-464c-908c-5a6092f4d8edn%40googlegroups.com.
DateTime.is?(a, operator, b), when operator :lt | :le | :eq | :ge | :gt, which would capture the :le and :ge options.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2ByT9jA7uqGX0Cyapgfx0AjW%2BU_d4Ai-NQ6vD9UsEb2uQ%40mail.gmail.com.
I'm not sure the name is right, but I likeDateTime.is?(a, operator, b), when operator :lt | :le | :eq | :ge | :gt, which would capture the :le and :ge options.As a usage api, we could actually have `compare?/3` especially as the name doesn't overlap with `compare/2` which would hopefully alleviate anyones concerns about the return type changingOn Mon, 31 Oct 2022, at 6:23 AM, José Valim wrote:
My thought process is that a simple to use API should be the focus, because we already have a complete API in Date.compare/2 and friends.
On Mon, Oct 31, 2022 at 02:16 Simon McConnell <simonmcconnell@gmail.com> wrote:
would we want on_or_after? and on_or_before? as well then? Or something like DateTime.is?(a, operator, b), when operator :lt | :le | :eq | :ge | :gt, which would capture the :le and :ge options.On Monday, 31 October 2022 at 7:26:42 am UTC+10 José Valim wrote:Thank you!A PR that adds before?/after? to Time, Date, NaiveDateTime, and DateTime is welcome!On Sun, Oct 30, 2022 at 6:46 PM Cliff <notcliff...@gmail.com> wrote:I did a bit of research. Many other languages use some form of operator overloading to do datetime comparison. The ones that do something different:
- Java has LocalDateTime.compareTo(other), returning an integer representing gt/lt/eq. There is also LocalDateTime.isBefore(other), LocalDateTime.isAfter(other), and LocalDateTime.isEqual(other). The LocalDateTime.is{Before, After} methods are non-inclusive (<, >) comparisons. They are instance methods, so usage is like `myTime1.isBefore(myTime2)`
- OCaml's "calendar" library provides a Date.compare function that returns an integer representing gt/lt/eq (for use in OCaml's List.sort function, which sorts a list according to the provided comparison function). It also provides Date.>, and Date.>=, etc. Worth noting is that OCaml allows you to do expression-level module imports, like Date.(my_t1 > my_t2) to use Date's > function in the parenthesized expression without needing to open Date in the entire scope ("open" is OCaml's "import") - this could potentially be possible in Elixir using a macro?
- Golang: t1.After(t2), t1.Before(t2), t1.Equal(t2). Non-inclusive (> and <).
- Clojure clj-time library: (after? t1 t2), (before? t1 t2), and (equal? t1 t2). IMO the argument order is still confusing in these.
On Sunday, October 30, 2022 at 3:15:14 AM UTC-4 José Valim wrote:I am definitely in favor of clearer APIs.However, it would probably be best to explore how different libraries in different languages tackle this. Can you please explore this? In particular, I am curious to know if before/after mean "<" and ">" respectively or if they mean "<=" and "=>" (I assume the former). And also if some libraries feel compelled to expose functions such as "after_or_equal" or if users would have to write Date.equal?(date1, date2) or Date.earlier?(date1, date2), which would end-up doing the double of conversions.
--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/fcd07389-c6a0-497d-9c09-7f1eacf620c6n%40googlegroups.com.
--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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/e6c55604-c3ea-464c-908c-5a6092f4d8edn%40googlegroups.com.
--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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2ByT9jA7uqGX0Cyapgfx0AjW%2BU_d4Ai-NQ6vD9UsEb2uQ%40mail.gmail.com.
--
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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/2e821e87-6ee0-4702-b69f-e2616b61b1dd%40app.fastmail.com.
I wonder how much of the issue is the Api and how much of the issue is just the docs? I.e its not a given that all arguments in every position always make sense, but we typically rely on things like elixir_ls to help us when the answer isn't obvious.
Could we perhaps just improve the docs in some way? i.e update the specs to say `datetime :: Calendar.datetime(), compares_to :: Calendar.datetime()`, and have the args say `compare(datetime, compares_to)` and have part of the first line of text say something a bit more informative?On Mon, Oct 31, 2022 at 3:02 AM, Jon Rowe <ma...@jonrowe.co.uk> wrote:I'm not sure the name is right, but I likeDateTime.is?(a, operator, b), when operator :lt | :le | :eq | :ge | :gt, which would capture the :le and :ge options.As a usage api, we could actually have `compare?/3` especially as the name doesn't overlap with `compare/2` which would hopefully alleviate anyones concerns about the return type changingOn Mon, 31 Oct 2022, at 6:23 AM, José Valim wrote:My thought process is that a simple to use API should be the focus, because we already have a complete API in Date.compare/2 and friends.
On Mon, Oct 31, 2022 at 02:16 Simon McConnell <simonmc...@gmail.com> wrote:would we want on_or_after? and on_or_before? as well then? Or something like DateTime.is?(a, operator, b), when operator :lt | :le | :eq | :ge | :gt, which would capture the :le and :ge options.On Monday, 31 October 2022 at 7:26:42 am UTC+10 José Valim wrote:Thank you!A PR that adds before?/after? to Time, Date, NaiveDateTime, and DateTime is welcome!On Sun, Oct 30, 2022 at 6:46 PM Cliff <notcliff...@gmail.com> wrote:I did a bit of research. Many other languages use some form of operator overloading to do datetime comparison. The ones that do something different:
- Java has LocalDateTime.compareTo(other), returning an integer representing gt/lt/eq. There is also LocalDateTime.isBefore(other), LocalDateTime.isAfter(other), and LocalDateTime.isEqual(other). The LocalDateTime.is{Before, After} methods are non-inclusive (<, >) comparisons. They are instance methods, so usage is like `myTime1.isBefore(myTime2)`
- OCaml's "calendar" library provides a Date.compare function that returns an integer representing gt/lt/eq (for use in OCaml's List.sort function, which sorts a list according to the provided comparison function). It also provides Date.>, and Date.>=, etc. Worth noting is that OCaml allows you to do expression-level module imports, like Date.(my_t1 > my_t2) to use Date's > function in the parenthesized expression without needing to open Date in the entire scope ("open" is OCaml's "import") - this could potentially be possible in Elixir using a macro?
- Golang: t1.After(t2), t1.Before(t2), t1.Equal(t2). Non-inclusive (> and <).
- Clojure clj-time library: (after? t1 t2), (before? t1 t2), and (equal? t1 t2). IMO the argument order is still confusing in these.
On Sunday, October 30, 2022 at 3:15:14 AM UTC-4 José Valim wrote:I am definitely in favor of clearer APIs.However, it would probably be best to explore how different libraries in different languages tackle this. Can you please explore this? In particular, I am curious to know if before/after mean "<" and ">" respectively or if they mean "<=" and "=>" (I assume the former). And also if some libraries feel compelled to expose functions such as "after_or_equal" or if users would have to write Date.equal?(date1, date2) or Date.earlier?(date1, date2), which would end-up doing the double of conversions.
--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/fcd07389-c6a0-497d-9c09-7f1eacf620c6n%40googlegroups.com.
--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/e6c55604-c3ea-464c-908c-5a6092f4d8edn%40googlegroups.com.
--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/CAGnRm4%2ByT9jA7uqGX0Cyapgfx0AjW%2BU_d4Ai-NQ6vD9UsEb2uQ%40mail.gmail.com.
--
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/2e821e87-6ee0-4702-b69f-e2616b61b1dd%40app.fastmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/7435b979-d0eb-4726-aa65-a94ada53d320n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/2ca24f84-0a77-4dcc-8917-83ef18bba16an%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/89619070-2b42-409a-bdeb-1259375f7f14n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2BPces0tbWQeZr-iuC%2BZWEyugRJ_9Op8d6oKZ6MmsQAkQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/7d7af1d6-bff7-4fbe-a3c4-0da74ac104d9n%40googlegroups.com.
if you want to try out an example of this, I wrote an
experimental implementation six years ago:
https://github.com/Qqwy/elixir-experimental_comparable
It has exactly the problem José describes: Defining a mutual comparison between `n` different datatypes requires `2*n*n` protocol module definitions.
An alternative approach some languages (such as Ruby) take, is to
allow the definition of a coercion to a compatible
datatype with sufficient detail.
This requires `n*(n-1)` definitions (one for each pair of
different types, but order does not matter).
The result can be re-used for multiple operations rather than
being hard-coded for comparison only.
An Elixir implementation of this can be found in
https://github.com/qqwy/elixir-coerce
You can check out https://github.com/Qqwy/elixir-number for an
example of usage. (Not for comparison but for calculation).
~Qqwy / Marten
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JmCeCXGHGmHgO0y3bK3P5z1YcoDuZnb3e_%3DtOzZxd24w%40mail.gmail.com.