[swift-evolution] [Pitch] New collection-based 'repeat' API

Brent Royal-Gordon brent at architechies.com
Fri May 12 04:31:50 CDT 2017

> On May 1, 2017, at 8:29 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> Heck, we don't even have left padding for strings!

For what it's worth, a good Swift implementation of left-padding would probably *use* `repeatElement`:

	extension String {
		mutating func leftPad(to intendedCount: Int, with char: Character = " ") {
			let extraCount = max(intendedCount - count, 0)
			let extras = repeatElement(char, count: extraCount)
			insert(contentsOf: extras, at: startIndex)

And, for that matter, `repeatElement` could be used to implement "repeat this collection":

	repeatElement(c, count: n).joined()

So I agree with you, Xiaodi, that there's no rush to do this. If we change our minds later, we can always turn `Repeated<T>` into a generic typealias for `RepeatedCollection<CollectionOfOne<T>>`.

Brent Royal-Gordon

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170512/93f2deb1/attachment.html>

More information about the swift-evolution mailing list