4 ms·
It's a bit longer in C++ but frankly not by that much : #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #inclu
by jcelerier 7y ago
It's a bit longer in C++ but frankly not by that much :
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <nlohmann/json.hpp>
#include <range/v3/action/sort.hpp>
int main()
{
using namespace nlohmann;
using namespace ranges;
const json parsed = json::parse(std::ifstream("/tmp/json/test.json"));
std::vector agenda(parsed.begin(), parsed.end());
sort(agenda, {}, [] (const auto& j) { return j["name"]; });
for(const auto& people : agenda) try {
std::tm t{};
std::istringstream(people["hired"].get<std::string>()) >> std::get_time(&t, "%Y-%m-%d %H:%M:%S");
std::cout << people["name"] << " (" << people["age"] << ") - " << std::put_time(&t, "%d/%m/%y") << ": \n";
for(const auto& email : people["emails"])
std::cout << " - " << email << "\n";
} catch (...) { }
}
- SaxonRobber 7y agoIt also requires a library outside of the standard.
- jcelerier 7y agodoes that matter ? Python does not even have an ISO standard
- SaxonRobber 7y agoI think it does, I should not require an external dependency for JSON. Having standard JSON support is far far more useful than another micro optimizations.
- jcelerier 7y agofifteen years ago that would have been XML and twenty-five years ago SGML and in ten years it'll be TOML or protobuf or something so...
- SaxonRobber 7y agoEventual deprecation is not a valid justification for making current technology less useful. Now I would buy your argument if C++ were as conservative as C, but it clearly isn’t. It keeps trying to push itself as a modern general purpose language, while ignoring what it actually takes to be useful as one.
- deleted 7y ago[deleted]
- rfoo 7y agoJava and Perl does not have json in builtin libs, does it make Java/Perl less useful or less expressive than Python?
- steveklabnik 7y agoTo be clear, my solution also included two libraries outside of the standard library. That’s idiomatic for Rust.
- BiteCode_dev 7y agoThis is getting ridiculous. We went from: > many Python projects have way more lines of code than equivalent C++ projects To: > It's a bit longer in C++ but frankly not by that much With the proof being literally __twice__ more characters, with one line being more than a 100 characters long. I understand that C++ doesn't have a native JSON lib, and sorting is a bit out there, but giving 3rd party lib access to Python feels like cheating: import pandas as pd agenda = pd.read_json("agenda.json", convert_dates=["hired"]) for _, (name, age, hired, emails) in agenda.sort_values("name").iterrows(): print(f"{name} ({age}) - {hired:%d/%m/%y}:") for email in emails: print(f" - {email}") I mean, I can also create a lib that does the entire script, install it, and just do 'import answernh; answerhn.print_json("agenda.json")' Now again, this is not so say "Python is better than C++". That's not the point. If you want to write a game engine, C++ makes sense, verbosity doesn't matter, and you don't want the Python GC to kick in. If you want to iterate on your Saas product API quickly, it's probably not the best choice. Why pretend otherwise? What the point of stating the sky is red?
- detaro 7y agoI think it shows quite well that the expressiveness difference is not as a large as many would think. Also, the difference in tone towards jcelerier compared to steveklabnik seems not warranted here. Both IMHO contributed an educational example. Your response here equally does (or does not) apply to the Rust version, doesn't it?
- BiteCode_dev 7y agoRust is fantastically expressive for a low level language. And modern C++ is not 15 years old C++. You have auto, lambda, etc. I just think it's silly to try to pretend to be more expressive than scripting languages. It's like Schwarzenegger trying to pretend it's as flexible as gymnast. What for? The guy is amazing at what he does.
- jcelerier 7y ago> I just think it's silly to try to pretend to be more expressive than scripting languages. So far we have a Python, a Rust and a C++ program in this thread which output the same thing given the same input, which, by the definition of expressive power of programming languages means that they are exactly as expressive as each other (given this information only and notwithstanding small differences such as error handling in this case). Also you mentioned in your original thread : > Even if the syntaxes were exactly the same (which they are not, python syntax is on average shorter) indeed > the low level nature of C++ requires your code to do operations that Python does need to do, such as memory management. Notice that there is nothing which looks like memory management in my code. There are entire classes of programs that can be written without thinking about when to allocate or free your memory at all - just declare your variables on the stack and with std types, and that is handled for you (and that's not a modern C++ thing ! it's been like that since the very beginning). Opting in to memory management is an active, conscious effort in these cases (or a residue of people who only were taught java / c# at school and sprinkle their code with `new`).