Javascript can't reverse strings
-
I was on a Windows PC, with no Python interpreter installed or any other language I know. I needed a quick way to reverse a long string. I could probably have used one of the several online pages that do that, or called @pythonbot (if it worked), but in a stroke of inspiration I realized every computer has a built in programming environment: the browser developer tools. So I switched to Firefox, pressed F12 and tried to guess the function (since I've never actually programmed in Javascript):
>"myverylongstring".reverse() TypeError: "myverylongstring".reverse is not a function >reverse("myverylongstring") ReferenceError: reverse is not defined
No? Fine, let's google it...
First result tells you to use
s.split("").reverse().join("");
. Second result tells you to not do that, and use their library instead. WTF? There really is no simple way to reverse a string in Javascript! You actually need an external library to properly handle the unicode combining characters? Isn't that one of the basic string operations?And it's not like it's some language that hasn't gotten any updates since the 80s. It has been getting additions continuously for the last decade and a very recent major revision by Mozilla that added many new functions (but not this one). This wouldn't even break any backwards compatibility.
-
Isn't that one of the basic string operations?
hmm.... ok.
Question: outside of academia and job interviews, what real world need is there to reverse an arbitrary string?
-
Question: outside of academia and job interviews, what real world need is there to reverse an arbitrary string?
Encryption. Duh.
-
Question: outside of academia and job interviews, what real world need is there to reverse an arbitrary string?
None.
Out of interest, I looked into whether the .NET Framework has built-ins to reverse strings. And it does… sort if. You have to use
Microsoft.VisualBasic.Strings.StrReverse()
;String
doesn't have a reverse method. And the VB method fucks up combining diacritics.
-
Question: outside of academia and job interviews, what real world need is there to reverse an arbitrary string?
Agreed, but splitting into graphemes probably still has real usecases. And after that
s.splitToGraphemes().reverse().join("")
works.
-
splitting into graphemes probably still has real usecases
Text-to-speech immediately springs to mind
-
Encryption. Duh.
serious or facetious?
cause i'm not seeing how that would help encryption. not that JS is good at the bitbashing that most encryption needs anyway..
so, if serious please explain.
String doesn't have a reverse method.
hmm... that is what i thought.
Agreed, but splitting into graphemes probably still has real usecases.
i'll agree with that, but then once you have the graphmemes you have a list not a string and reversing a list does have use cases that warrant a builtin function.
-
what real world need is there to reverse an arbitrary string?
Maybe he had a lot of words he wanted to check for palindromes? ;)
-
Maybe he had a lot of words he wanted to check for palindromes? ;)
There are more efficient ways that don't require the allocation of two strings
*awaits whoosh*
-
There are more efficient ways that don't require the allocation of two strings
Wasn't specified as a requirement
-
There are more efficient ways that don't require the allocation of two strings
There are more efficient languages than javascript.
-
-
There really is no simple way to reverse a string in Javascript! You actually need an external library to properly handle the unicode combining characters? Isn't that one of the basic string operations?
Uh no? Why would it be?
-
Text-to-speech immediately springs to mind
Text rendering is more important; that's what really needs graphemes. TTS needs phonemes, which are something else. (It also requires higher-level information to do well, such as intonation hints.)
-
will any of these work?
The fourth one isn't too bad, but still isn't a true whoosh. Needs more sense of air flowing over the top of one's head.
The other ones just sucked…
-
..... this could take quite a bit of searching for just the right sound clip.
-
..... this could take quite a bit of searching for just the right sound clip.
What about this one?
http://youtu.be/98Vkl1YnNFs?t=1m20s
-
Text rendering is more important; that's what really needs graphemes. TTS needs phonemes, which are something else. (It also requires higher-level information to do well, such as intonation hints.)
But surely you need the graphemes to work out the correct phonemes? For non-dictionary words, at least.
-
that's a nice woosh sound.....
-
splitToGraphemes()
congratulations on being the ONLY (at this time) Google result for that.
-
You're TRWTF, it takes about 20 seconds to install Python. I think that it took me about a minute to install three different versions on my new laptop this week.
-
It takes me more than 20 seconds to get the <URL of the> Python archive from my package manager.
-
It takes me more than 20 seconds to get the <URL of the> Python archive from my package manager.
We all know that you're a lil' slow, Ben.
-
-
I guess you don't want to hear about how I was getting 25MBps on my phone outside my apartment this weekend.
Note: we know all you Europeans get gigabit speed on your old Nokia phones, so you don't have to tell us.
-
I guess you don't want to hear about how I was getting 25MBps on my phone outside my apartment this weekend.
Note: we know all you Europeans get gigabit speed on your old Nokia phones, so you don't have to tell us.
I get 26 at my new office!
-
I might also get 26, depending on the unspecified unit.
-
I might also get 26, depending on the unspecified unit.
Does Go have type inference?
-
There is only One True Whoosh.
https://www.youtube.com/watch?v=ksQCQgb36Fw&t=5m33s
-
congratulations on being the ONLY (at this time) Google result for that.
I thought Discourse didn't get (reilably) indexed?
-
I thought Discourse didn't get (reilably) indexed?
It does. I'll tell you a secret. I actually paginates quite well if you turn off the JavaScript (not sure whether it can post too, guessing not, but spiders don't need that).
-
But surely you need the graphemes to work out the correct phonemes? For non-dictionary words, at least.
It's… non-trivial. The classic example of why this is so is:
It gets even worse when you consider languages that don't use the Latin alphabet. Look, language is a complete bugger. People are the cause of this.
-
@thegoryone said:
It is in php -
strrev()
but honestly, doesn't that just speak volumes for PHP?
Don't forget that this name comes from the fundamental C "string" handling functions...
-
It's also in Python, Java, Ruby and SQL.
-
@thegoryone said:
I literally haven't touched C since first year of University
I haven't touched C since five minutes ago...
-
It's also in Python, Java, Ruby and SQL.
I don’t think any of these languages have a “reverse string” function which properly handle combinining Unicode characters, though.
-
It gets even worse when you consider languages that don't use the Latin alphabet.
Actually English is one of the worst languages for this. Most languages using Latin and similar (languages written in Cyrillic and Greek have quite regular pronunciation) have more regular pronunciation and in most languages with other writing system the writing system is better match for the phonemes used, so in most cases their rules are also mostly regular (ideographic systems like Chinese may have many characters, but reading them does not need much context (only one other character IIUC)). The only obvious examples that may be worse then English are Arabic and Hebrew that normally omit vowels, but on the other hand I believe they still generally write consonants regularly.
-
Don't forget that this name comes from the fundamental C "string" handling functions...
Yes, and it's completely Discoursistent with it. str_replace vs strpos for example.
That and the fact that no, it won't handle multi-byte characters properly. If you want to byte-reverse your string, go nuts. If you want to reverse your string in PHP for some definition of properly, you start fucking about with regexp to split the string. And you're still probably SOL with combining diacritics.
-
And you're still probably SOL with combining diacritics.
If you want to handle them right, start by normalizing that string to NFC. It will reduce the headache level quite a lot.
-
It's… non-trivial. The classic example of why this is so is:
Of course, one of the first things that page says is ". Linguists have pointed out that the location of the letters in the constructed word is inconsistent with how those letters would be pronounced in those placements, and that the expected pronunciation in English would be "goaty".[4] For instance, the letters "gh" cannot be pronounced /f/ at the beginning of a syllable, and the letters "ti" cannot be pronounced /ʃ/ at the end of a syllable."
-
If you want it really secure, do
String encrypted = MyString.Reverse().Reverse();
-
Yes, and it's completely Discoursistent with it. str_replace vs strpos for example.
Neither of those are C functions, though strpos() is a replacement for strchr() and strstr() which would be less useful in php.
-
Neither of those are C functions, though strpos() is a replacement for strchr() and strstr() which would be less useful in php.
I wasn't claiming they were, merely that they did have C functions and they were not even consistent with them.
You begin to understand why PHP really is TRWTF.
-
Then again, glibc has both
strfry()
("randomize a string") andmemfrob()
("frobnicate (encrypt) a memory area"). The latter xor:s a memory area with 42. (FWIW, it comes with a warning that it's not a proper encryption routine, though).Filed under: I have written untold numbers of
memfry()
equivalents.
-
You begin to understand why PHP really is TRWTF.
We should write a book, really. Or at least a megathread. I'll start, @Arantor can share his additional (and superior to mine) knowledge as I hit upon certain areas which are more broken than I realized :P
-
We should write a book, really. Or at least a megathread. I'll start, @Arantor can share his additional (and superior to mine) knowledge as I hit upon certain areas which are more broken than I realized
I found out just today that while http://php.net/manual/en/class.exception.php claims the Exception class has always had 3 arguments to its constructor, the version of php in redhat 5 only accepts 2.
The fact we're still using RHEL5 as a development platform may be an auxillary WTF.
-
Then again, glibc has both
strfry()
("randomize a string") andmemfrob()
("frobnicate (encrypt) a memory area"). The latter xor:s a memory area with 42. (FWIW, it comes with a warning that it's not a proper encryption routine, though).Filed under: I have written untold numbers of
memfry()
equivalents.PHP has str_shuffle but naturally only safe for single-byte encodings.
-
We should write a book, really. Or at least a megathread. I'll start, @Arantor can share his additional (and superior to mine) knowledge as I hit upon certain areas which are more broken than I realized :P
So, where do you start? Everything in PHP is to some degree broken or Discourse-levels of consistent.
-
I found out just today that while http://php.net/manual/en/class.exception.php claims the Exception class has always had 3 arguments to its constructor, the version of php in redhat 5 only accepts 2.
The fact we're still using RHEL5 as a development platform may be an auxillary WTF.
Congratulations, you have managed to discover a whole new level of PHP-is-TRWTF.
-
Did you try looking at the constructor documentation which states that the third arg was added in PHP 5.3.0?