Re:C++11 and Boost - Succinct like Python
by BoppreH on Wednesday October 31, 2012 @01:01PM
(#4723851)
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.
Re:C++11 and Boost - Succinct like Python
by wting on Wednesday October 31, 2012 @01:18PM
(#4723962)
`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)
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.
In most programming languages the [] operator allows one to access elements in a container.
In this case, we are accessing the position "two colons minus one". I refuse to believe this makes any sort of sense for someone that has no deep knowledge of Python.
Re:C++11 and Boost - Succinct like Python
by mquander on Wednesday October 31, 2012 @06:12PM
(#4725183)
Lots of languages use [] with colon separators for array slicing, e.g. F#, Perl, Ruby. The idea that this syntax involves "deep knowledge" is laughable.
Your criticism is analogous to me looking at "int* foo = bar()" and saying, hey, we're multiplying a type by a name! How ridiculous!
The int pointer syntax is in fact ridiculous, so it does not support your point. Also s[x:y] is intuitively understandable compared to referencing two colons and minus one...
Well, that is ridiculous. It makes the language literally impossible to parts without context-sensive semantic analysis. Go Language fixes that bug by always having a type name first or a colon after introducing a variable.
Your username is a little too on the nose here. Plus you seem to have somehow entirely missed my point, to a degree I wonder if it is deliberate. Is this some sort of troll?
Re:C++11 and Boost - Succinct like Python
by alpatters on Wednesday October 31, 2012 @01:38PM
(#4724089)
In the spirit of the article, boost range also has a reverse filter, so the following would return a reversed range similar to python's generator expression
using namespace boost::adaptors;
s | reversed
or reverse(s)
Re:C++11 and Boost - Succinct like Python
by anonymous on Wednesday October 31, 2012 @01:14PM
(#4723943)
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
by newobj on Wednesday October 31, 2012 @01:02PM
(#4723856)
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?
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 and C++ systems programming so I use both frequently and while I prefer C++, I think Python is the best scripting language available today.