Created
September 23, 2015 07:21
-
-
Save pram/cc22945c26e43629aa28 to your computer and use it in GitHub Desktop.
Scala Reversing Strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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