5 ms·
'sorted()' doesn't really act like that, does it - taking in a comparator function? It's much better to have comparator functions for the most common types bak
by barrkel 14y ago
'sorted()' doesn't really act like that, does it - taking in a comparator function?
It's much better to have comparator functions for the most common types baked in, and use projection to select a list of fields to use to compare with.
Comparator functions are very easy to get wrong. The example given here - ".sorted((a, b) -> a.getValue() - b.getValue())" - won't work properly when the calculation wraps around.
- masklinn 14y ago> It's much better to have comparator functions for the most common types baked in, and use projection to select a list of fields to use to compare with. Except that requires a sorting key for the result. Trivial for dynamically typed languages, but how do you handle the type of that key in a statically typed language? Mandate that the key be a string? Build a limited set of overloads against a dedicated type? And even then, how do you specify the relative constraints of the different values within that key, especially within the limitations of java's type system? > The example given here - ".sorted((a, b) -> a.getValue() - b.getValue())" - won't work properly when the calculation wraps around. But that's less an issue of "comparator functions [being] very easy to get wrong" and more an issue of "integer is an asinine type to use as ordering result". Even more so in a statically typed language. Haskell uses a dedicated enumerated type[0] which does not have this issue. [0] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Ord.html#t:Ordering http://hackage.haskell.org/packages/archive/base/latest/doc/...
- barrkel 14y agoI don't know what you mean by "sorting key" in this context. The idiom I have in mind is how .Net implements "SortBy", "ThenBy" and friends. Overloads can be used for a comparator function based approach, but it should not be the first choice. And I'll stand by comparators being easy to get wrong, particularly when values may be null, polymorphic, etc.
- batista 14y ago>I don't know what you mean by "sorting key" in this context. He means the item by which you sort. The "by" in your "SortBy" example.
- barrkel 14y agoYes, but that reading doesn't make sense given what he also wrote - that apparently this would make things more awkward in a static language.
- batista 14y agoHow do you define the sorting key in a static language? With reflection? e.g if anArray is a Java array of Employee objects, and you want to sort on their salary attribute, how do you specify it? anArray.sortBy(???); In a dynamic language it doesn't matter much, since all attribute access amounts to (and is as slow as) reflection on a static language anyway, so you can do something like, say: anArray.sortBy("salary");
- Peaker 14y agoIn Haskell, sortBy takes a comparison function: sortBy :: (a -> a -> Ordering) -> [a] -> [a] And Ordering is defined as: data Ordering = LT | EQ | GT If you want to provide a "key" function rather than a comparison function, you can use sortOn: sortOn :: Ord b => (a -> b) -> [a] -> [a] Which is basically just a sortBy with a function that applies the key function and then compares as normal.
- eru 14y agoAnd everything is statically typed.
- spullara 14y agoIn JDK8 there are method references that could enable this. For example, you can reference a method in a class like Person::age ( equivalent to (Person person) -> person.age() ) that could then be used to sort the collection since Integer implements Comparable.
- heretohelp 14y agoI have to agree here, this feels too much like: "Doctor, it hurts when I move my arm like this!" "So don't move your arm like that." We've spent the last...god knows how many decades dealing with wrap-around on (unsigned generally) integers. :|
- eru 14y ago> Trivial for dynamically typed languages, but how do you handle the type of that key in a statically typed language? Haskell handles this just fine. Static types are only a problem, if they are weak and/or your language syntax is cumbersome. Automatic type inferences helps, of course.