> Hahaha. This has to be satire right?
Nope.
/home/nate/src/github.com/juju/juju$ grep -r ") Less(" . | wc -l
67
(granted, 10 are under the .git directory, so I guess 57)But in any other language, we'd still have the same 57 definitions of how to sort a type.... we'd just have 3 fewer lines of boilerplate for each of those (which live off in the bottom of a file somewhere and will never ever need to change).
That claim turns out to not be the case.
Translate this into whatever language you like:
Machine {
Name string
OS string
RAM int
}
You have 3 places that want to sort a list of machines, one by name, one by OS, and one by RAM. You're telling me there's a language that can do that without having to write some kind of code like this for each? sort(machines, key: Name)
I don't understand how that's possible, but I welcome your explanation.If you're always going to sort them based on some (other) relation between the fields, make your type a custom instance of Ord, e.g. "instance Ord Machine where compare = compare `on` name".
To sort the same type with distinct comparators, you'll obviously need to distinguish them, as in e.g. "osSort = sortBy (compare `on` os)".
That's a straw man: nobody is telling you that.
But, you are being told there are multiple languages where you can sort machines by their fields without having to write
type byName []Machine
type byOS []Machine
type byRAM []Machine
func (a byName) Len () int {
return len(a)
}
func (a byOS) Len () int {
return len(a)
}
func (a byRAM) Len () int {
return len(a)
}
func (a byName) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byOS) Swap(i, j int) {
a[i], a[j] = a[i], a[i]
}
func (a byRAM) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byName) Less(i, j int) bool {
return a[i].Name < a[j].Name
}
func (a byOS) Less(i, j int) bool {
return a[i].RAM < a[i].RAM
}
func (a byRAM) Less(i, j int) bool {
return a[i].OS < a[j].OS
}
sort.Sort(byName(machines))
sort.Sort(byOS(machines))
sort.Sort(byRAM(machines))
For example, in Julia, you can just do sort(machines, by=m->m.Name)
sort(machines, by=m->m.OS)
sort(machines, by=m->m.RAM)
The equivalent is possible in any almost modern language. E.g. in C# machines.OrderBy(m=>m.Name)
machines.OrderBy(m=>m.OS)
machines.OrderBy(m=>m.RAM)
In Haskell sortBy (comparing Name) machines
sortBy (comparing OS) machines
sortBy (comparing RAM) machines
This is an extensively solved problem in modern programming languages.As a bonus, the opportunity for making an error each time you want to sort a new type of array in a new way is reduced if you only have to write one line of code each time.
// Unreachable based on the rules of there not being duplicate
// environments of the same name for the same owner, but return false
// instead of panicing.
return false
Guess what, I worked with a sort function with the same kind of assumptions, but the implicit rules was broken: the "should never happen" path happened (names were not unique, after all). I found about that only after I wrote my own sort which was careful enough to check that the order was indeed total and when results diverged for some tests.
I really disliked that because sorting was an important part in that tool (maybe it is not in yours).