Live data from Hacker News

C++11 and Boost - Succinct like Python

fendrich.se

61–70 of 172 posts

Re: C++11 and Boost - Succinct like Python

#61
post #58

I find C++ much easier to read than Python. Here's how to reverse a string in C++: string s = "string"; reverse(s.begin(), s.end()); And here's how to reverse a string in Python: s = "string" print s[::-1] In my opinion, the C++ version is far easier to read and just makes more sense. Edit - This is just one small example, however, I find it holds true for the entire languages in general. Also, I do a lot of Python a…

Actually, how to reverse a string in Python is just "s[::-1]" -- you omitted the printing from the C++ version.

In terms of readability, they both beg the question - is the reversed string ever actually held in memory at some point or can it occur lazily via latent reverse iteration?

I mean is the alleged readability of either language really answering any important questions?

Re: C++11 and Boost - Succinct like Python

#62
post #53
post #48

Earlier quoted context omitted.

For ord he could use std::to_string string ord(string const &s) { return to_string(unsigned(s[0])); } He skips all the const-refs...

Yes, to_string would have been better The C-style cast is shorter, but less safe, so you should not use it: http://stackoverflow.com/questions/1609163/what-is-the-diffe... Yeah, I couldn't quite decide if I should include const refs. On the one hand, it is idiomatic, but on the other hand I didn't want to clutter the code with something that is just an optimization on paper.

I know the C-style casts are bad (although this is more of a C++-style-C-style cast. (unsigned) would have been the C-cast. But still bad). But in this situation it should be safe and for use in an example more readable.

btw. Please fix either the size of your code or the margins of your blog. You use only half the screen width but I still have to scroll sideways in the code listing.

Re: C++11 and Boost - Succinct like Python

#63
post #58

I find C++ much easier to read than Python. Here's how to reverse a string in C++: string s = "string"; reverse(s.begin(), s.end()); And here's how to reverse a string in Python: s = "string" print s[::-1] In my opinion, the C++ version is far easier to read and just makes more sense. Edit - This is just one small example, however, I find it holds true for the entire languages in general. Also, I do a lot of Python a…

How many other languages do you know? You sound like you know C++ really well and sort of know Python, and aren't clearly distinguishing between abstract readability (as ill-defined as that is) vs. how well you personally know the code you are reading. What's probably wrong with your Python reading is that you have to look up what the third parameter in the slice more than the slice parameter actually being confusing.

Re: C++11 and Boost - Succinct like Python

#64
post #57

I think people who jump on the "C++11 is as good as Python/Ruby/Javascript/other-language-of-the-week" bandwagon are missing the point, and that is that limitations are a good thing. Can C++ be made as concise and easily-readable as one of the above languages? I wouldn't doubt it. C++ can be made to do lots of things, and it doesn't achieve that by being elegant. It achieves that by trying to do everything possible u…

Is there really such a bandwagon?

Re: C++11 and Boost - Succinct like Python

#65
post #21
post #16

Earlier quoted context omitted.

Go is certainly a fine language. One common instance where I would prefer C++, though, is when you are writing libraries for others to use. A language that runs on a VM can not easily be used as a library inside a language that uses another VM. You cannot easily write a library for Python in Go or vice versa. If you use a VM-less language like C or C++, your work can be used (through various wrappers, like SWIG or th…

Doesn't Go compile to machine code as well without a VM in between?

It has a runtime statically linked. All linking is static and std call interfaces are not exposed so you cannot expose libraries to other language bindings.

Re: C++11 and Boost - Succinct like Python

#66
post #34
post #27

It's succint, all right (well, sort of). But Python's major strength is readability, even more than coinciseness, or better, to provide both at the same time. C was born as a terse language, sacrificing readability for coinciseness (the original examples in K&R are incredibly succint, almost elegant, but far from readable). I can't see many improvements in C++ (a language that arguably has worse coinciseness than C,…

I call shenanigans. That's true of simple scripts, I'm sure. But you can't seriously claim to me that you can understand decorator idioms, iterables or list comprehensions without a deep understanding of the language. What you say might have been true for Python c. 1998, it certainly isn't true today. Broadly: reading code is just hard. It's much harder than writing code. There are no non-trivial codebases that can b…

Here's an example: A co-worker looked at a function I wrote that converts a table to a tree, and he said to me, "I thought Python was supposed to be readable, but I have no idea what this is doing."

See for yourself: https://gist.github.com/3988350

Re: C++11 and Boost - Succinct like Python

#67
post #58

I find C++ much easier to read than Python. Here's how to reverse a string in C++: string s = "string"; reverse(s.begin(), s.end()); And here's how to reverse a string in Python: s = "string" print s[::-1] In my opinion, the C++ version is far easier to read and just makes more sense. Edit - This is just one small example, however, I find it holds true for the entire languages in general. Also, I do a lot of Python a…

In python you can also do

  ''.join(reversed(s))
It's still not the same as your example, because in C++ you reverse the string in-place. If you have a mutable array-like, you can do

  arrayish[:] = reversed(arrayish)
But to me the syntax with `s[::-1]` is already perfectly clear. There is the difference that `reversed` returns an iterator, while `s[::-1]` constructs a new string.

Re: C++11 and Boost - Succinct like Python

#68
post #59
post #58

I find C++ much easier to read than Python. Here's how to reverse a string in C++: string s = "string"; reverse(s.begin(), s.end()); And here's how to reverse a string in Python: s = "string" print s[::-1] In my opinion, the C++ version is far easier to read and just makes more sense. Edit - This is just one small example, however, I find it holds true for the entire languages in general. Also, I do a lot of Python a…

Or you could use the "reversed" built-in function: s = "string" reversed(s) It also has the plus of creating a generator, saving memory from a new array allocation.

`reversed(s)` returns an iterator. The much slower equivalent of `s[::-1]` is `''.join(reversed(s))`.

It's odd how Python has list.reverse() but not str.reverse(). Probably a conscious design decision sometime ago.

Re: C++11 and Boost - Succinct like Python

#70
post #68
post #59

Earlier quoted context omitted.

Or you could use the "reversed" built-in function: s = "string" reversed(s) It also has the plus of creating a generator, saving memory from a new array allocation.

`reversed(s)` returns an iterator. The much slower equivalent of `s[::-1]` is `''.join(reversed(s))`. It's odd how Python has list.reverse() but not str.reverse(). Probably a conscious design decision sometime ago.

list is mutable while str is not. So reverse() should return a new copy, but it would duplicate the functionality of s[::-1] (which is quite idiomatic, although a bit obscure)
Post reply on HN