Skip to content

Instantly share code, notes, and snippets.

@akr4
Created July 20, 2011 11:00
Show Gist options
  • Save akr4/1094761 to your computer and use it in GitHub Desktop.
Save akr4/1094761 to your computer and use it in GitHub Desktop.
Scala 練習問題 #daimonscala
import scala.annotation.tailrec
class StringSupport(s: String) {
def reverseOrder: String = s.toList.foldLeft(Nil: List[Char])((result, c) => c :: result).mkString
def reverseCase: String = s.toList.foldRight(Nil: List[Char])((c, result) => c match {
case c if c.isUpper => c.toLower :: result
case c if c.isLower => c.toUpper :: result
case c => c :: result
}).mkString
}
object StringSupport {
implicit def convert(s: String): StringSupport = new StringSupport(s)
}
import StringSupport._
println("AbcDEf123".reverseOrder)
println("AbcDEf123".reverseCase)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment