5 ms·
Note: my C++ is extremely rusty. Right now, I think that's approximately, vector<tuple<int, string>> output; transform( somelist.begin(), some
by deathanatos 9y ago
Note: my C++ is extremely rusty.
Right now, I think that's approximately,
vector<tuple<int, string>> output;
transform(
somelist.begin(), somelist.end(),
back_inserter(output),
[](const auto &f) { return make_tuple(f.weight, f.name); }
);
sort(output.rbegin(), output.rend());
Ranges, I believe, would reduce this a lot, possibly even to a single line. If I am reading the docs on it correctly, something like,
vector<auto>(somelist | view::transform([](const auto &f) { return make_tuple(f.weight, f.name); })) | action::sort;
I think.
Two notes, however:
1. I feel like most of the desire for a static language is to know what type something is. Is C++ exactly as brief as Python? No, as I think you've demonstrated. But I think you're a lot more likely to know the type of something. Rarely do I think I find that Python has annotations, and annotations can be wrong.
2. C++ is, in general, I feel, much more explicit about where copies occur. I elided one of the copies in your example, opting instead for an in-place sort (but this is trivial to fix in the Python).
- mehrdadn 9y ago> vector<auto> Really? O.o How could this possibly work?
- deathanatos 9y agoOh, that was probably a typo (it was late); replace that with a concrete type.
- lmm 9y ago> 1. I feel like most of the desire for a static language is to know what type something is. Is C++ exactly as brief as Python? No, as I think you've demonstrated. But I think you're a lot more likely to know the type of something. Sure, but you don't have to choose between them, there are plenty of languages where you can have both Pythonic terseness and full type safety. E.g. Scala: (for {k <- somelist} yield (k.weight, k.name)).sorted.reverse Many other strongly typed (ML-like) functional languages are similar.