-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add maximumWith and minimumWith #109
Conversation
What did you use Also, maybe theres a better name than |
and
and
so:
seemed to fit, but I get your point about Concerning the use case, I use it to get the maximum from a |
That makes sense about the naming. A lot of times it just feels like none of the names are good, at the very least we can be consistent with So what do you do to compare |
It have this:
and implemented |
I feel like you could do that just as cleanly with the following:
(See https://ellie-app.com/3bW9vNzH4SSa1 if you want to play around with it.) That is not to say that there are no valid use cases for |
Fair enough. This requires to go through the list three times if |
How do you end up using Also I think I am a bit confused. So in your project, suppose you have a list |
Well, this may become embarrassing because I actually use it for a quite obscure and specific other
This function gives the index of the first element in the lists that differs, or
I could have made a custom type to differentiate the case where the lists are equal but it was not needed for my use case, so I just used a But then I need the same but between one list and a list of lists. The returned index is the maximum one found, and as you can guess,
Example:
I'm not sure that this will help in this discussion though 😅 |
Maybe a better example would be an imaginary card game where a Jocker scores 0 point but beats all other cards:
Then
|
Yeah, I guess that is pretty reasonable. Would you mind benchmarking your implementation against the following implementation? My guess is that inlining the accumulation function will speed things up.
|
We are lucky, I used a simple list of numbers:
|
Previous benchmarks were done without So I think I should change the patch to use |
Yeah, updating the PR to use inline accumulator with case... of seems reasonable to me. |
Actually, could you run one last benchmark? I wonder if we can improve speed even more by pulling the accumulation function out into it's own top-level definition. |
I'm not sure to understand all those results and I don't really have time now to look at the generated js, but performance is a lot worse with a top level definition, about -28% on firefox and -50% on chrome... I tested the following function with
|
Hmm that's pretty interesting. Let's just go with the inline version then. Don't worry about analyzing the results, most of perf is tough to account for anyway. |
Ok. I have updated the patch. |
Okay awesome. I feel good enough about these functions now. Do you all think its ready to merge now? For me at least, in your card game example, its not clear that your Also, thats interesting that the case statement is so much faster than the if statement. I wonder why. |
Okay, if I dont hear otherwise I am going to merge and publish this soon. Thanks @rlefevre for the contribution. |
As Core
List
hassortWith
andsortBy
andlist-extra
maximumBy
andminimumBy
, I thought it could be relevant to add those (I needed amaximum
function with a custom comparison function in my app).Note that similar functions already exist in
NoRedInk/elm-compare
(in 0.18 only for now), so this may be the reason why they were not inlist-extra
. They are however more difficult to find because of theComparator
alias, and they do not return the first maximum/minimum element but the last:https://package.elm-lang.org/packages/NoRedInk/elm-compare/latest/Compare#maximum