Last active
August 29, 2015 14:18
-
-
Save shizone/52e37d51656ac75fa25f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defn reverse [x] | |
(loop [result nil acc (vec x)] | |
(if (empty? acc) | |
(apply str result) | |
(recur (conj result (first acc)) | |
(rest acc))))) | |
(reverse "qwerty") | |
"ytrewq" |
This file contains hidden or 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
object Reverser { | |
def reverse(result: List[Char], acc: List[Char]): String = { | |
if (acc.isEmpty) { | |
result.mkString | |
} else { | |
reverse(acc.head :: result, acc.tail) | |
} | |
} | |
def reverse(target: String): String = reverse(Nil, target.toList) | |
} | |
Reverser.reverse("qwerty") | |
"ytrewq" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scalaで書きなおしたらなんかさっくりできたのでまあそんなもんか…