[elixir-core:9136] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread Tyson Buzza
I have been using DateTime and NaiveDateTime alot over the last year and have found that using the compare function to involves some mental gymnastics. When using compare you often just want the most recent or oldest timestamp, or else you want to sort a list of timestamps. I am proposing four

Re: [elixir-core:9136] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread José Valim
I love this. My suggestion is to add earliest(date, date) and earliest(list_of_dates) and the same for latest. We need to add it to Time, Date, NaiveDateTime and DateTime. A PR is very appreciated. *José Valim* www.plataformatec.com.br Skype: jv.ptec Founder and Director of R&D On Thu, Oct 10,

Re: [elixir-core:9137] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread José Valim
Oh, I see why you called it sort_earliest, because you also want to allow a function to be given, in the style of sort_by. I am not a big fan of sort_earliest though. What if we add only: DateTime.earliest(list_of_dates) and DateTime.earliest(list_of_dates, fun)? You can always get the earliest of

Re: [elixir-core:9138] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread YongHao Hu
If Tyson Buzza don't mind, I would love to write this as my first PR as I had used Elixir in work for two years and want to do a contribution. : P On Thu, Oct 10, 2019 at 4:29 PM José Valim wrote: > Oh, I see why you called it sort_earliest, because you also want to allow > a function to be give

Re: [elixir-core:9140] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread Tyson Buzza
I'm in a similar situation to you YongHao. Sorry, but would you mind if I did it? To be clear the functionality would be: DateTime.earliest(enumerable, mapper \\ fn x -> x end) with a typespec like @spec earliest(Enum.t(), (any() -> t())) :: list() right? On Thursday, October 10, 2019 at 4:4

Re: [elixir-core:9140] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread Wojtek Mach
I think earliest and latest are good because they are descriptive but wondering if they should be called min and max instead. The advantage of the latter is they’d be similar to Kernel.min/2 and Enum.min/1,2. I guess a disadvantage is someone reading `min` could think it returns the minimum allo

Re: [elixir-core:9141] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread Wojtek Mach
Should Date.earliest operate on `Date.t` or `Calendar.date`? That is, should we allow the following? iex> Date.earliest([~N[2019-01-01 09:00:00], ~D[2019-01-02]) ~D[2019-01-01] Seems like this could be useful. -- You received this message because you are subscribed to the Google Group

Re: [elixir-core:9142] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread José Valim
> > To be clear the functionality would be: > > DateTime.earliest(enumerable, mapper \\ fn x -> x end) > > with a typespec like > > @spec earliest(Enum.t(), (any() -> t())) :: list() > > right? > Correct. -- *José Valim* www.plataformatec.com.br Skype: jv.ptec Founder and Director of R&D -- Y

Re: [elixir-core:9143] [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread José Valim
Good catch. It should operate on Calendar.date, as all other functions in the Date module. Btw, the reason I prefer earliest/latest is exactly because it has semantic meaning. It also avoids any confusion from people thinking the regular min/max work with calendar types (as you said). On Thu, Oct

[elixir-core:9145] Re: [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread Wiebe-Marten Wijnja
Playing devil's advocate here for a minute: The wanted functionality could also be reached using Enum.sort(enumerable, &(DateTime.compare(&1, &2) in [:lt, :eq])) for `DateTime.earliest` and similarly: Enum.sort(enumerable, &(DateTime.compare(&1, &2) in [:eq, :gt])) for `DateTime.latest`. Arg

[elixir-core:9146] Re: [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread Ben Wilson
FWIW We have an earliest_date and latest_date helper in almost every Elixir app we've built. The Enum.sort solution requires requires too many leaps to be at a glance readable if it's been a bit since you used DateTime.compare. Definitely a fan of including this. On Thursday, October 10, 2019 a

Re: [elixir-core:9146] Re: [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread José Valim
Yes, I am with Ben. I have considered adding similar APIs already but I could not come up with a good proposal. Now I am quite happy with the current one. -- *José Valim* www.plataformatec.com.br Skype: jv.ptec Founder and Director of R&D -- You received this message because you are subscribed

Re: [elixir-core:9148] Re: [Proposal] latest and earliest functions for DateTime and NaiveDateTime

2019-10-10 Thread Fernando Tapia Rico
I would drop the `mapper` function, and have a similar interface to `Enum.min/2`: `DateTime.earliest(list_of_dates, empty_fallback // fn -> raise(...) end)`. -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group an