-
Notifications
You must be signed in to change notification settings - Fork 21
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
Two suggestions about Ordering #4
Comments
Thanks for the feedback! Regarding #1 actually the current solution is more type-safe. With the current solution you must give a lambda which will return always string or always number. But yes we need to allow returning strings for minOn (and maxOn etc) besides number. We can probably have a type alias like
Although I'm not sure if it's worth it. I'm not sure allowing booleans buys us much though. I'll think about #2 and answer soon on that one too. |
I've now commited to master ToOrderable and it's used for maxOn & minOn as well: Let me know what you think about that. I'll look at the second part of your bug ASAP. |
so, my plan for the second issue is to keep the sorting functions as they are (I'm already not so happy about having both sortOn and sortBy). But prelude would offer a new function: export function fieldsOrderingFn( Prelude already exports slightly similar functions, like fieldsHashCode: I basically coded it now, but I need to write apidoc & tests. In your case that would enable:
Let me know if you think there's a flaw with that idea. Otherwise I'll push that today I think. |
I'm also interested if you have another idea for the name of that new function. |
I would add booleans. If someone what to sort by predicate why not? Put something to the end or to the start of the list is pretty reasonable case Does
Javascript array |
Ok you convinced me about adding booleans for the sorting, I'll add it. My first thought was, if you're interested in sorting with
there is an objective advantage, and one that I had in mind when I came up with that, although it's really not a very strong one. That's related to the types, in a bit the same way as I was saying that With the type that I suggested, you must give a series of lambdas. Each one must return a string. Or a number. While if you give an array, then each element of the array will contain string|number. And there we are again. In theory (and yes it's a bit stretching, but it's possible), the user could provide a function which will return a string for the second element once, and another time a number. And it'll type-check. When I compare element-by-element in prelude's code, I'll have to either cast to any (trust the user was disciplined), or have an if, potentially even decide to throw if I refuse type mismatches. I like |
Not necessary. For example I just want to send emails to gmail last for some reason.
I don't think it would be a problem in real life code and I think the array version looks more idiomatic because of sorting something by tuple, but on the other hand everyone can create their own function like that so library could offer more type-safe version by default. One thing bother me. |
Actually I started thinking, if we do multi-criteria sorting, inevitably we'll want to sort on some things descending. I'm pretty sure it's possible to achieve: const sortingFn = compareBy(x=>x.a).thenByDesc(x=>x.b); with also compareByDesc and thenBy. I'll have a go at it tonight my time, in like 12h, probably. |
I think
|
I'd really prefer to have sortOn & sortBy as simple as possible & put that complexity elsewhere. I like the path I'm taking with that. Remember what also you said: with this, you can write your own functions with arrays if you prefer, or whatever, you're not tied to prelude implementing something or not. Also, this complexity must then be replicated in maxOn, minOn, sortOn, and other places presumably. |
@user471 you're right about sortOn :-) I'll implement it as you suggested 👍 |
…n an ascendant or descendent manner.
I understand that. Complex functions could be overwhelming, but personally I prefer when I press dot type
Do you mean other collections? Maybe It is possible to use some mixins so there wouldn't be any duplication? |
OK I think master now has all the fixes. Let me know if you see something's off. I want to add one more change and then I'll release 0.7.2. |
About your last comment: you were right about sortOn. Looking at the purpose of sortOn and sortBy it was obvious this did fit in sortOn. I implemented the way you suggested: list.sortOn(x=>x.a,{desc:x=>x.b}) |
thanks |
just published version 0.7.2 with the changes we discussed! thank you for the feedback & suggestions! |
1)Right now there are
Maybe It should be something like
It's possible to use sortBy
But I think something like this could be better
or maybe even
The text was updated successfully, but these errors were encountered: