Skip to content

Instantly share code, notes, and snippets.

@shizone
Last active August 29, 2015 14:18
Show Gist options
  • Save shizone/52e37d51656ac75fa25f to your computer and use it in GitHub Desktop.
Save shizone/52e37d51656ac75fa25f to your computer and use it in GitHub Desktop.
(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"
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"
@shizone
Copy link
Author

shizone commented Apr 8, 2015

Scalaで書きなおしたらなんかさっくりできたのでまあそんなもんか…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment