by jamesaguilar · 12y ago · 0 comments
In a few other languages:
// C++
sort(vs.begin(), vs.end(),
[](const string& l, const string& r) -> bool {
return l.size() < r.size();
});
# Python
vs.sort(key=lambda s: len(s))
When they fix this, Go might be the perfect language for me. Until then, I'm not touching it with a pole.There's plenty of other ways in which Go is shorter than the equivalent C++ (e.g. with type inference, or channel operations), but no-one's trying to claim that the shortest code wins.
It's not just sorting, it's any parametric polymorphism: generic operations or non-built-in containers (not just in the sense of "array", an atomic ref is also a container) for instance. Go only lets do these in verbose, clunky ways, and losing much of the type safety Go is supposed to provide (what with being a statically typed language)
> no-one's trying to claim that the shortest code wins.
No one is indeed, so that's an irrelevant strawman.
What else could it be? I guess it's a little clunkier, and it's true that writing generic containers in Go isn't so great, but is that sufficient to elicit a reaction of "I'm not touching it with a pole"? Nobody claims it's perfect, but is it really sufficiently horrible to warrant that?
That's one hell of an understatement.
> is it really sufficiently horrible to warrant that?
A number of people obviously and repeatedly say that yes, a statically typed language without parametric polymorphism is sufficiently painful to use something else instead.
sort(begin(vs), end(vs),
[](auto const& l, auto const& r) {
return l.size() < r.size();
});
Even simpler.dfrey already asked about why begin/end are functions now.
I see auto being used inside the lambda. Was that not allowed in C++11? I got sucked into the "use auto everywhere" mantra, so I just imagined it was possible, but could easily see if it were not. That is probably the biggest decrease in size to the original code posted.
I see return type inference. That is one thing I have heard about being new in C++14. To be honest, I would rather see the explicit return type there. Inference is always nice, though.
It wasn't:
Can any "idiomatic" Go programmers here give a better example of how you would do this?
Sure.
sort.Sort(sort.Reverse(sort.StringSlice(vs)));
type StringSliceByLength struct {
sort.StringSlice;
}
func (s StringSliceByLength) LessThan(i, j int) bool {
return len(s.StringSlice[i]) < len(s.StringSlice[j]);
}
Sort.sort(StringSliceByLength(vs))Instead of methods, you could of course write normal functions taking []string, but only methods can fulfil interfaces.
vs.sort(key=len)2. My solution is not incomplete. It solves the stated problem exactly. If you need to sort the objects with a different criteria, implement the interface with whichever criteria you want. It's not so difficult. Generics will not save you from having to implement some kind of comparison criteria in other languages.
3. You can add more methods to the interface for an out of place sort.
4. If you need to implement a b-tree, add the methods to whichever type you need to sort appropriately.
It's not that big of a problem. You will not have to up and down cast. You only have to cast a type of interface{} to your type once and only once while you are comparing.
Is your problem ideological or specific to this example?
// Sort a slice of structs with first class functions.
type Album struct {
Title string
Year int
}
albums := []Album{
{"Born to Run", 1975},
{"WIESS", 1973},
{"Darkness", 1978},
{"Greetings", 1973},
}
less := func(a, b Album) bool { return a.Year < b.Year },
sorted := QuickSort(less, albums).([]Album)
For more fun things, see: http://godoc.org/github.com/BurntSushi/ty/fun L.sort(key=len)