I don't like `repeat` when you actually use the in...
# getting-started
k
I don't like
repeat
when you actually use the index, it reads wrong to me. So I'd replace that with a for loop instead. I always like the
StringBuilder
/
apply
combo, so that's fine IMO.
s
curious that there isn’t a
buildString(String) { ... }
there’s one that accepts an int and a lambda, but it just uses the int to set the initial capacity
h
i guess i could easily write one....
k
Turns out there is a
buildString
with exactly this behavior!
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/build-string.html
h
oh my!
k
Ah wait you were talking about the prefix part, my bad I completely misunderstood what you were saying.
s
lmao no worries
k
Actually now I'm even more confused, what would you want
buildString(String) { ... }
to do?
s
StringBuilder(String)
takes a string to initialize the builder -
buildString
could do the same
k
Hmm I see.
h
is there any advantage to stringbuilder over just string arithmetic like +?
s
mostly just the methods that the builder provides
they’re pretty convenient and, being named, they can be more descriptive, depending on the the code it’s being used in
if you’re building using a loop, then StringBuilder is a lot faster
when you’re just adding strings together, the StringBuilder and the
+
approach are the same - the compiler will transform the latter into the former
but the same optimization isn’t performed in a forEach or anything like that
(at least, I’m pretty certain it’s not 😅)