title: Opa: What Is the Fastest Way to Reverse a String?
body: <p>What is the most performant way to implement a "reverse a string" function in Opa?</p>
  
  <p>I am aware of <code>String.reverse()</code>. It does get the job done, but its source code is bound to some external function. I want to understand what the best (fastest) tools are for reordering a string.</p>
  
  <p>So, for example, this works, but isn't particularly fast:</p>
  
  <pre><code>reverse(s:string) =
    p = parser
      | x=(.) xs=(.*) -&gt; Text.concat(reverse(Text.to_string(xs)), x)
      | x=(.*) -&gt; x
    Parser.parse(p, s)
  </code></pre>
  
  <p>How can I make this perform better?</p>
  
  <h3>Edit: Another Implementation Based on akoprowski's Answer</h3>
  
  <p>I think this is what he has in mind. This one is much faster than the parser version, but the built-in <code>String.reverse</code> is the way to go if all you need to do is reverse a string.</p>
  
  <pre><code>reverse_list(l:list(string)):list(string) =
    match l with
    | [x|xs] -&gt; reverse_list(xs) ++ [x]
    | _ -&gt; []
  
  reverse_string(s:string) =
    string_list = List.init((i -&gt; String.get(i, s)), String.length(s))
    String.flatten(reverse_list(string_list))
  </code></pre>
  
  <h3>Edit: The Above Implementation Is Not Ideal</h3>
  
  <p>Check out the accepted answer for a better option.</p>