<div dir="ltr">On Mon, May 1, 2017 at 9:34 PM, Karl Wagner via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><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&lt;T&gt; 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">&gt; 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">&gt; 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&lt;C: Collection&gt; 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/<wbr>karwa/<wbr>5228974a0b4dfd000a916f0aac2721<wbr>c6</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) -&gt; RepeatCollection&lt;<wbr>CollectionOfOne&lt;Self&gt;&gt;</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) -&gt; RepeatCollection&lt;Self&gt;</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) -&gt; RepeatCollection&lt;Self&gt;</font></div></div></blockquote><div><div><br></div><div>- We would replace the existing Repeated&lt;T&gt; type with a typealias to RepeatCollection&lt;<wbr>CollectionOfOne&lt;T&gt;&gt;</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">&gt; for i in CollectionOfOne(1).<wbr>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">&gt; 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">&gt; 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">&gt; 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">&gt; String(“yellow”.characters.<wbr>dropFirst().dropLast().repeat(<wbr>times: 3))</font></div><div><font face="Courier">“elloelloello&quot;</font></div><div><br></div><div><br></div></blockquote>Thoughts?</div></div></blockquote><div><br></div><div>OK, now as to your proposed APIs themselves, here are some critiques:</div><div><br></div><div>The issue you&#39;ve identified with the cumbersome nature of CollectionOfOne shows why repeatElement is currently a top-level function and intentionally so. In brief, there&#39;s nothing special about Collection to make it the obvious type on which to provide a `repeated` method. The _result_ of that operation is a collection, but there&#39;s no reason the argument has to be. The correct &quot;type&quot; on which to provide that method would be Any, IMO, but of course we cannot provide extensions on Any. In general, in such scenarios, the consistent design choice in the standard library is to provide a free function.</div><div><br></div><div>Here, it is not inconsistent to _add_ something for repeating the elements in a collection (to Collection itself) without also stuffing the existing `repeatElement` functionality into Collection. TBH, the latter seems like an orthogonal topic included for the express purpose of eliminating top-level functions, without addressing the underlying reason for the existence of these top-level functions in the first place (no extensions on Any). So again, unrelated and worth setting aside, IMO.</div><div><br></div><div>Other minor points include that `repeatElements` doesn&#39;t meet standard API naming guidelines. It should be `repeatingElements`. You also intuitively tacked on a `times` argument label in the example usage even though your proposed API doesn&#39;t have it. It suggests that `repeat[ing]Elements(3)` is actually quite ambiguous: repeat the value 3, repeat the whole collection 3 times, or repeat so that the final count is 3? Better to have the label always, I should think. Another point is that it&#39;d take some justification to include both flavors of `repeat[ing]Elements`, as repeating until a final count is trivially composed with new one-sided ranges: `[1, 2, 3].repeatingElements(times: .max)[..&lt;4]`.</div><div><br></div><div><br></div></div></div></div>