golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.7k stars 17.62k forks source link

proposal: strings, bytes: Add JoinSeq #70034

Open earthboundkid opened 2 hours ago

earthboundkid commented 2 hours ago

Proposal Details

61901 added SplitSeq and friends to bytes, but nothing for joining. Right now you have to do slices.Collect first or use a bytes.Buffer/strings.Builder, which feels unfortunate. I propose to add strings.JoinSeq(seq iter.Seq[string], sep string) string and the bytes equivalent as well.

gabyhelp commented 2 hours ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

seh commented 2 hours ago

Do you imagine that it would collect all the items from the sequence first to figure out the total length of the final string before concatenating them?

earthboundkid commented 1 hour ago

I imagine it would just use a strings.Builder and opt into whatever the default buffer there is. Maybe there could be optimization to use a slice for sequences shorter than N.