Phrase reversals: Difference between revisions

Frink
No edit summary
(Frink)
Line 952:
Reversed words => attesor edoc esarhp lasrever
Reversed order => reversal phrase code rosetta
</pre>
 
=={{header|Frink}}==
The built-in <CODE>reverse</CODE> function reverses a string or the elements of a list.
 
Frink's built-in <CODE>reverse[''string'']</CODE> is quite smart and uses a grapheme-based algorithm to handle Unicode correctly. That is, it preserves "user-perceived characters" that may consist of characters, combining accents, high-plane Unicode characters (that is, above U+FFFF,) surrogate pairs, ''etc.'' correctly.
 
Many languages will not work correctly with upper-plane Unicode characters because they are represented as Unicode "surrogate pairs" which are represented as two characters in a UTF-16 stream.
 
For example, the string "g\u0308o" represents a g with combining diaeresis, followed by the letter o. Or, in other words, "g̈o". Note that while there are three Unicode codepoints, only two "graphemes" are displayed. Using Frink's smart "reverse" function preserves these combined graphemes. A naive reverse would move the diaeresis over the o instead of the g.
 
Frink also has smart, human-language aware functions for splitting a string into a list of words. It correctly handles hyphenated words, numbers, and punctuation. It also has a built-in smart <CODE>reverseWords</CODE> function to reverse the words in a string.
 
<lang frink>a = "rosetta code phrase reversal"
println[reverse[a]]
println[join["", map["reverse", wordList[a]]]]
println[reverseWords[a]] // Built-in function
// Alternately, the above could be
// join["", reverse[wordList[a]]]
</lang>
 
{{out}}
<pre>
lasrever esarhp edoc attesor
attesor edoc esarhp lasrever
reversal phrase code rosetta
</pre>
 
490

edits