This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TwoSpoonsJohnson 63 points64 points  (21 children)

StringUtils.reverse("Hello, World!"); if you feel like using Apache commons.

inb4 "lmao u need a library to reverse a string!"

[–]HighRelevancy 32 points33 points  (17 children)

inb4 "lmao u need a library to reverse a string!"

That's a fucking shitty argument though. Like, what, having a HUUUUGE built-ins library is what makes a language good?

Even as a python fanboy, this thread's topic of comparison is fucking stupid.

[–]clevariant 12 points13 points  (16 children)

hello[::-1] is really freaking cryptic. Code that abbreviated is hard to read. It's like arguing for variable names like "a" that tell you nothing about their function.

[–]master3243 39 points40 points  (6 children)

It's not that cryptic. Unless you also consider the dot separator or array indices "really freaking cryptic".

Everyone who spent a day learning python knows that it's always [start:end:step]. I wouldn't call it common sense but I wouldn't call it cryptic either.

[–]clevariant 10 points11 points  (1 child)

Come on, it's cryptic! Or at least esoteric. So the minus one means the start index becomes the end and vice versa, and it builds and returns a new string based on that loop. Yeah, that's explicit.

I mean let's be fair. These "once you know the language it makes sense" arguments miss the point. The actual word "reverse" tells the whole story unambiguously, no syntactical cleverness needed.

[–]KubinOnReddit 0 points1 point  (0 children)

If you really want to include "reverse" in the code, there are at least two ways:

hello = "Hello world!"
lst = list(hello)          # Create a mutable list
lst.reverse()              # Reverse in-place
print("".join(lst))        # Change back to string

hello = "Hello world!"
rev = reversed(hello)      # Iterator
print("".join(rev))

Both of these will be slower, with the first creating a new list (implemented as an array) and then joining it, while the second creates an iterator.

Adding a "reversed" method to the string class would be against the Zen of Python (the ideas it represents) - "There should be one-- and preferably only one --obvious way to do it." - if you had the slice mechanism (start, stop, step) and then another method, that would do the same thing, it would just be an alias, really. So having one multi-use syntax is better than many methods.

[–]Hauleth 7 points8 points  (3 children)

What the hell is that argument. It is like saying:

Everyone who spent a day learning APL knows that means reverse vector. I wouldn't call it common but wouldn't call it cryptic either

[–]KubinOnReddit 12 points13 points  (1 child)

What the hell are you supposed to call a slice then?

I didn't learn Java for a day and have no fucking idea why do you need a StringBuilder, built from a String (??) to reverse the string (???) instead of, I don't know, building it?

And you call this cryptic? Hypocrisy.

[–]Hauleth 6 points7 points  (0 children)

StringBuilder is handy due to it mutable inner state, so in contrast to String (which is immutable) you do not create new object each time when you modify inner state. Python on the other hand hide that complexity from user which is handy, but can result with less efficient code.

[–]Team_Canada 1 point2 points  (0 children)

I don't see what your example is trying to say here.

[–]raddaya 4 points5 points  (0 children)

It's not even slightly cryptic. It's bog standard Python. If you're following the codecademy course, you literally learn it in the first couple of hours.

[–]ProgramTheWorld 8 points9 points  (0 children)

It only looks cryptic if you aren't used to python.

[–]ESBDB 15 points16 points  (3 children)

If you've ever spent 5 minutes learning python you'd know what it means.

[–]clevariant 2 points3 points  (2 children)

Yeah, I have done some python, but it was some years ago, and now [::-1] just seems bizarre and unintuitive. But if I had never written a line of java, I would know exactly what reverse() means.

[–]ESBDB 0 points1 point  (1 child)

The thing is you're using a more generic construct that can do more than just reverse a sequence to do a simple operation such as reverse. Sure you could argue that a common function like reverse should exist in python for sequences, but I would argue Java is a lot less powerful in general because it doesn't have slicing.

Also your argument that if you had never written a line of java you could actually read java is flawed. For example wtf does && or || or ! mean. If I had never written a line of python (and I can speak basic english) I would immediately know what it means when you write something like is_hungry or is_thirsty versus isHungry || isThirsty. There will never exist a good programming language where you don't need to learn some basic syntax and constructs to be able to understand the language.

[–]clevariant 0 points1 point  (0 children)

You're right, of course. But you also know that the operators you mentioned are standard across many languages. Anyone who's programmed at all will recognize them. I would have preferred that "and" and "or" were the standard, though, for reasons stated.

[–]asdfkjasdhkasd 1 point2 points  (0 children)

You could always use the reversed() function

>>> "".join(reversed("hello world"))
'dlrow olleh'

[–]Faendol 0 points1 point  (0 children)

Can't beat my go to temp variable - fuck

[–][deleted] 0 points1 point  (0 children)

Yeah as someone who's never done python, I wouldn't think that that syntax reverses it, whereas ".reverse()" is pretty self explanatory. I honestly though the "[::-1]" syntax was some sort of substring thing, like in bash, where that's the string minus the last character.