Allan Wang
09/27/2020, 9:29 AMList<Char>
to String
or should we just use joinToString
? I’m trying to shuffle letters in a word and I’m doing so by String.toList().shuffled()
Animesh Sahu
09/27/2020, 11:51 AMStringBuilder(list.size).apply { list.forEach { append(it) } }.toString()
Animesh Sahu
09/27/2020, 11:53 AMjoinToString("")
does the same, but has a lot more checks to append more elements like limit, prefix, postfix, separator, and value transformer. ^ this will just remove all those unnecessary check made during concatenation with joinToString(""). Also pre-specifying length will pre-allocate size of underlying array so no resizing is required thereafter.Nir
09/27/2020, 3:17 PMNir
09/27/2020, 3:17 PMNir
09/27/2020, 3:18 PMAnimesh Sahu
09/27/2020, 3:18 PMNir
09/27/2020, 3:18 PMNir
09/27/2020, 3:20 PMAnimesh Sahu
09/27/2020, 3:20 PMNir
09/27/2020, 3:20 PMAnimesh Sahu
09/27/2020, 3:20 PMpublic fun <T> Iterable<T>.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String {
return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()
}
And joinTo as:
public fun <T, A : Appendable> Iterable<T>.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): A {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
buffer.appendElement(element, transform)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
return buffer
}
Nir
09/27/2020, 3:20 PMNir
09/27/2020, 3:21 PMAnimesh Sahu
09/27/2020, 3:21 PMNir
09/27/2020, 3:23 PMNir
09/27/2020, 3:23 PMNir
09/27/2020, 3:24 PMNir
09/27/2020, 3:25 PMAnimesh Sahu
09/27/2020, 3:25 PMNir
09/27/2020, 3:26 PMNir
09/27/2020, 3:26 PMAnimesh Sahu
09/27/2020, 3:28 PMAnimesh Sahu
09/27/2020, 3:29 PMNir
09/27/2020, 3:30 PMNir
09/27/2020, 3:31 PMNir
09/27/2020, 3:31 PMNir
09/27/2020, 3:32 PMNir
09/27/2020, 3:33 PMnanodeath
09/27/2020, 3:47 PMnanodeath
09/27/2020, 3:48 PMshuffle
method for arrays, apparentlynanodeath
09/27/2020, 3:49 PMAnimesh Sahu
09/27/2020, 3:59 PMfun CharArray.shuffle(): CharArray {
for (i in indices) {
swap(i, Random.nextInt(size))
}
return this
}
private fun CharArray.swap(from: Int, to: Int) {
val tmp = get(from)
set(from, get(to))
set(to, tmp)
}
Then use the primitive CharArray:
val shuffledString = String("Hello World".toCharArray().shuffle())
Nir
09/27/2020, 4:00 PMNir
09/27/2020, 4:02 PMNir
09/27/2020, 4:02 PMnanodeath
09/27/2020, 4:04 PMString.toList().shuffled().joinToString(separator = "")
is probably fine, and if you want a tiny bit more efficiency you can pass in a pre-sized StringBuilder (which has all already been discussed already). were it up to me I'd probably do one of those. maybe if it's a large string (>100 chars) I'd use the pre-sized StringBuilder.Nir
09/27/2020, 4:05 PMFleshgrinder
10/04/2020, 8:22 AMchar[]
you'll have an additional copy the moment you want to go back to String
because Java checks if it's Unicode. There are APIs that can do it without but they're all package scoped in the JDK and go through multiple hoops to keep them hidden. The UUID implementation is using them and that's why I was unable to beat it's formatting speed in Kotlin.