Last active
August 11, 2020 08:11
-
-
Save koji-k/4735700 to your computer and use it in GitHub Desktop.
Scala's mkString on Groovy.
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
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