Skip to content

Instantly share code, notes, and snippets.

@koji-k
Last active August 11, 2020 08:11
Show Gist options
  • Save koji-k/4735700 to your computer and use it in GitHub Desktop.
Save koji-k/4735700 to your computer and use it in GitHub Desktop.
Scala's mkString on Groovy.
class WieMkString {
static String mkString(List self){
mkString(self, "", "", "")
}
static String mkString(List self, String sep) {
mkString(self, "", sep, "")
}
static String mkString(List self, String start, String sep, String end) {
def str = start
if (self.size() > 0){ str += self.head().toString() }
self.tail().each{
str += "${sep}${it.toString()}"
}
str + end
}
}
def list = [*1..10]
use(WieMkString){
assert list.mkString() == "12345678910"
assert list.mkString("&") == "1&2&3&4&5&6&7&8&9&10"
assert list.mkString("<td>", ", ", "</td>") == "<td>1, 2, 3, 4, 5, 6, 7, 8, 9, 10</td>"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment