Derek Peirce
06/14/2023, 1:01 AMjoinToString
, but instead of a transform
lambda, it takes build: StringBuilder.(T) -> Unit
. That avoids creating intermediate strings for each element if multiple append
calls would be more efficient.Derek Peirce
06/14/2023, 1:10 AMStringBuilder
, but the loop has an isNotEmpty
check to decide whether or not to include the separator.ephemient
06/14/2023, 1:11 AMStringBuilder
receiver is too powerful, because the block can insert(0, ...)
and break the separationephemient
06/14/2023, 1:12 AMAppendable
though?Derek Peirce
06/14/2023, 1:13 AMAppendable
looks like it should work just as well.ephemient
06/14/2023, 1:13 AMDerek Peirce
06/14/2023, 1:14 AMephemient
06/14/2023, 1:14 AMephemient
06/14/2023, 1:15 AMthrows IOException
)Derek Peirce
06/14/2023, 1:22 AMappend
for primitives and Any
? Are those implemented more efficiently than calling toString
? I would doubt it.ephemient
06/14/2023, 8:41 AMentries.fold(StringBuilder()) { sb, (key, value) -> sb.append(key).append('=').append(value).append(", ") }.dropLast(2)
Dominaezzz
06/14/2023, 8:04 PMDerek Peirce
06/14/2023, 8:06 PMjoinToString
and buildString
manually? The goal is to not create intermediate strings and copying.Dominaezzz
06/14/2023, 8:07 PMDominaezzz
06/14/2023, 8:08 PMDerek Peirce
06/14/2023, 8:10 PMAppendable
preserves safety while matching the performance of a hand-written buildString
and loop.Derek Peirce
06/14/2023, 8:14 PMfold
can work, but it's much more difficult to read to understand what it's trying to do compared to joinToString
, or update it safely (the dropLast
must match the separator size), and I can't tell from the documentation how efficient StringBuilder.dropLast
actually is.Dominaezzz
06/14/2023, 8:21 PMDerek Peirce
06/14/2023, 8:28 PMStringBuilder.append(int)
still calls toString
, so with a few extensipn methods on Appendable
, we end up with the same API and the same number of strings.ilya.gorbunov
06/19/2023, 11:19 AM