Does this gain something by being part of the standard library as opposed to being built on top of it?<br><br><div class="gmail_quote"><div dir="ltr">On Mon, May 1, 2017 at 21:34 Karl Wagner via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Currently, we have the Repeated<T> type, which presents a single element as though it were a Collection. <div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="Courier">> for i in repeatElement(1, count: 3) { print(i) }</font></div><div><font face="Courier">1</font></div><div><font face="Courier">1</font></div><div><font face="Courier">1</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">> for i in repeatElement([1, 2, 3], count: 3) { print(i) }</font></div><div><font face="Courier">[1, 2, 3]</font></div><div><font face="Courier">[1, 2, 3]</font></div><div><font face="Courier">[1, 2, 3]</font></div></blockquote><div><br></div><div>However, we lack the ability for Collections to repeat their contents in a single list; basically, some kind of “flatMap” to repeatElement’s “map”. So I’d like to pitch a new API for repeated values. </div><div><br></div><div>- We would add a RepeatCollection<C: Collection> type, which loops over its base Collection a certain number of times (or until a maximum ‘count’). </div><div> Implementation might look something like this (<a href="https://gist.github.com/karwa/5228974a0b4dfd000a916f0aac2721c6" target="_blank">https://gist.github.com/karwa/5228974a0b4dfd000a916f0aac2721c6</a>), except that we’d add some optimised map(), filter() and contains() functions which apply the algorithm once to the base and multiply the result.</div><div><br></div><div>- We would add 3 new functions to all Collections:</div><div><br></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><font face="Courier">/// Repeats the collection <i>itself</i> N times.</font></div></div><div><div><font face="Courier">///</font></div></div><div><div><font face="Courier">func repeated(_ times: Int) -> RepeatCollection<CollectionOfOne<Self>></font></div></div><div><div><font face="Courier"><br></font></div></div><div><div><font face="Courier">/// Repeats the collection’s <i>contents</i> N times.</font></div></div><div><div><font face="Courier">///</font></div></div><div><div><font face="Courier">func repeatElements(_ times: Int) -> RepeatCollection<Self></font></div></div><div><div><font face="Courier"><br></font></div></div><div><div><font face="Courier">/// Loops the collection’s contents to present a Collection of length N.</font></div></div><div><div><font face="Courier">///</font></div></div><div><div><font face="Courier">func repeatElements(count: Int) -> RepeatCollection<Self></font></div></div></blockquote><div><div><br></div><div>- We would replace the existing Repeated<T> type with a typealias to RepeatCollection<CollectionOfOne<T>></div><div>- The existing, top-level repeatElement(T, Int) function <i>could</i> stay, but could also be replaced with an incantation on CollectionOfOne. I’m fairly ambivalent about this point - it’d be nice to see the function go, but the replacement also isn’t obvious.</div><div><br></div><div>Example usage of the new API:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="Courier">// Equivalent to repeatElement(1, count: 3)</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">> for i in CollectionOfOne(1).repeatElements(3).forEach { print(i) }</font></div><div><font face="Courier">1</font></div><div><font face="Courier">1</font></div><div><font face="Courier">1</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">// Equivalent to repeatElement([1, 2, 3], count: 3)</font></div><div><font face="Courier"><br></font></div><div><div><font face="Courier">> for i in [1, 2, 3].repeated(3).forEach { print(i) }</font></div><div><font face="Courier">[1, 2, 3]</font></div><div><span style="font-family:Courier">[1, 2, 3]</span></div><div><span style="font-family:Courier">[1, 2, 3]</span></div></div><div><span style="font-family:Courier"><br></span></div><div><span style="font-family:Courier">// New, flat repetition</span></div><div><font face="Courier"><br></font></div><div><font face="Courier">> for i in [1, 2, 3].repeatElements(3) { print(i) }</font></div><div><font face="Courier">1</font></div><div><font face="Courier">2</font></div><div><font face="Courier">3</font></div><div><font face="Courier">1</font></div><div><font face="Courier">2</font></div><div><font face="Courier">3</font></div><div><font face="Courier">1</font></div><div><font face="Courier">2</font></div><div><font face="Courier">3</font></div><div><font face="Courier"><br></font></div><div><div><span style="font-family:Courier">// New, flat repetition</span></div><div><font face="Courier"><br></font></div><div><font face="Courier">> for i in [1, 2, 3].repeatElements(count: 4) { print(i) }</font></div><div><font face="Courier">1</font></div><div><font face="Courier">2</font></div><div><font face="Courier">3</font></div><div><font face="Courier">1</font></div><div><br></div></div><div><font face="Courier">// Additional benefit: you can now repeat slices!</font></div><div><font face="Courier"><br></font></div><div><font face="Courier">> String(“yellow”.characters.dropFirst().dropLast().repeat(times: 3))</font></div><div><font face="Courier">“elloelloello"</font></div><div><br></div><div><br></div></blockquote>Thoughts?</div><div><br></div><div>- Karl<br><div><br></div></div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>