itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Add `join(prefix, sep, suffix) -> String`

Open ronanM opened this issue 1 year ago • 4 comments

Add a join() operator with prefix, sep and suffix.

assert_eq!([1, 2, 3].iter().join("<[", ", ", "]>"), "<[1, 2, 3]>");

ronanM avatar Aug 06 '24 16:08 ronanM

So this:

[1, 2, 3].iter().join("<[", ", ", "]>")

...would be a shorthand for this:

format!("<[{}]>", [1, 2, 3].iter().format(", "))

jswrenn avatar Aug 06 '24 16:08 jswrenn

I‘m pretty sure that - if we do this - we should aim for interleave_with_prefix_separator_suffix, ie. not tie it to strings.

Users then can still call join manually if they want.

I know it may be a bit annoying if prefix/separator/suffix have different types, but the concept seems not inherently related to text.

phimuemue avatar Aug 06 '24 17:08 phimuemue

So

iter.foo(start, sep, end)

...would be a shorthand for:

once(start).chain(iter.intersperse(sep)).chain(once(end))

That's quite a mouthful, and I think the argument for providing a shorthand for that is a bit stronger. Then again, I cannot think of a time where I've needed such a pattern outside of string formatting, and this shorthand works poorly for string formatting when the item type isn't a string.

jswrenn avatar Aug 06 '24 18:08 jswrenn

This makes me think of this conversation about Display adapters: https://users.rust-lang.org/t/joining-numbers-into-a-string/115540/7?u=scottmcm

scottmcm avatar Aug 06 '24 20:08 scottmcm