Normal and visual string reversion
05AB1E, 16 bytes
Uses the fact that 05AB1E has a constant predefined to "()<>[]{}"
and isn't affected the visually reversion.
Code:
,q‡"}{][><)("užR
Explanation:
, # Pop and print the input.
q # Quit.
‡"}{][><)("užR # This part is ignored.
Try it online!
Reversed:
Ržu"()<>[]{}"‡q,
Explanation:
R # Reverse the input.
žu # Short for "()<>[]{}".
"()<>[]{}" # Push this string.
‡ # Transliterate (no-op, since everything is transliterated to itself).
q # Quit and implicitly print.
, # This part is ignored.
Try it online!
Visually reversed:
Ržu")(><][}{"‡q,
Explanation:
R # Reverse the input.
žu # Short for "()<>[]{}".
")(><][}{" # Push this string.
‡ # Transliterate (giving the visually reversed string).
q # Quit and implicitly print.
, # This part is ignored.
Try it online!
Uses CP-1252 encoding.
CJam, 21 bytes
qe#ere$_"}{][><)("%Wq
Test it here.
Normal reversion:
qW%"()<>[]{}"_$ere#eq
Test it here.
Visual reversion:
qW%")(><][}{"_$ere#eq
Test it here.
Explanation
First, the normal code:
qe#ere$_"}{][><)("%Wq
This is simple: q
reads all input, e#
comments out the remainder of the program, and the input is printed implicitly at the end.
Now the normal reversion:
q e# Read all input.
W% e# Reverse it.
"()<>[]{}" e# Push this string.
_$ e# Duplicate and sort it. However, the string is already sorted
e# so we just get two copies of it.
er e# Transliteration (i.e. character-wise substitution). But since the
e# source and target string are identical, the reversed input
e# is left unchanged.
e#eq Just a comment...
And finally, the visual reversion:
q e# Read all input.
W% e# Reverse it.
")(><][}{" e# Push this string.
_$ e# Duplicate and sort it. This gives us "()<>[]{}", i.e. the
e# same string with each bracket pair swapped.
er e# Transliteration (i.e. character-wise substitution). This
e# time, this toggles all the brackets in the reversed input
e# completing the visual reversion.
e#eq Just a comment...
Haskell, 124 bytes
Forward:
f=id
--esrever.q pam=2>1|esrever=2<1|f;x=x q;')'='(' q;'('=')' q;']'='[' q;'['=']' q;'>'='<' q;'<'='>' q;'}'='{' q;'{'='}' q
Normal reverse:
q '}'='{';q '{'='}';q '>'='<';q '<'='>';q ']'='[';q '['=']';q ')'='(';q '('=')';q x=x;f|1<2=reverse|1>2=map q.reverse--
di=f
Visual reverse:
q '{'='}';q '}'='{';q '<'='>';q '>'='<';q '['=']';q ']'='[';q '('=')';q ')'='(';q x=x;f|1>2=reverse|1<2=map q.reverse--
di=f
Each version defines a function f
which takes and returns a string. In forward mode f
is the identity function id
, the rest of the code is a comment. In normal reverse mode the guard 1<2
in f
is True
, so reverse
is applied. In visual reverse mode, the <
is switched to >
and the guard is False
. The second guard is just the other way around and True
in visual mode, so additionally q
is applied which switches "()<>{}[]".
f|1<2=reverse|1>2=map q.reverse -- normal reverse mode
f|1>2=reverse|1<2=map q.reverse -- visual reverse mode
Besides <
and >
in the guards, my code doesn't use any of the brackets, so they can't be messed up.