Skip to content

Instantly share code, notes, and snippets.

@pram
Created September 23, 2015 07:21
Show Gist options
  • Save pram/cc22945c26e43629aa28 to your computer and use it in GitHub Desktop.
Save pram/cc22945c26e43629aa28 to your computer and use it in GitHub Desktop.
Scala Reversing Strings
def reverse_tail2(s: String): String = {
@scala.annotation.tailrec
def impl(ss: String, r: String): String = {
if (ss == null) return null
if (ss.tail.isEmpty) return ss.head + r
impl(ss.tail, ss.head + r)
}
impl(s, "");
}
def reverseString(s:String) = (for(i<-s.length-1 to 0 by -1) yield s(i)).mkString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment