<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">For reference, here are some links to
      previous discussions on related topics:<br>
      <br>
      1) Kevin Ballard in December 2015: <a
href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151228/004635.html">Proposal:
        CollectionType.cycle property for an infinite sequence</a><br>
      <br>
      2) Ben Cohen in February 2016: <a
href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170213/032120.html">Sequence/Collection
        Enhancements</a> (one of the enhancements that Ben proposed to
      consider for adding to the standard library is a <tt>cycle</tt>
      method for <tt>Collection</tt> and/or <tt>Sequence</tt>. I don't
      think there is a more specific proposal for this yet, but at least
      we know it's on the core team's radar.<br>
      <br>
      On 02.05.2017 04:34, Karl Wagner via swift-evolution wrote:<br>
    </div>
    <blockquote
      cite="mid:A52C9587-8FF0-42CE-86EA-267981BB76C2@springsup.com"
      type="cite">Currently, we have the Repeated&lt;T&gt; type, which
      presents a single element as though it were a Collection. 
      <div class="">
        <div class=""><br class="">
        </div>
        <blockquote style="margin: 0 0 0 40px; border: none; padding:
          0px;" class="">
          <div class=""><font class="" face="Courier">&gt; for i in
              repeatElement(1, count: 3) { print(i) }</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
          <div class=""><font class="" face="Courier">&gt; for i in
              repeatElement([1, 2, 3], count: 3) { print(i) }</font></div>
          <div class=""><font class="" face="Courier">[1, 2, 3]</font></div>
          <div class=""><font class="" face="Courier">[1, 2, 3]</font></div>
          <div class=""><font class="" face="Courier">[1, 2, 3]</font></div>
        </blockquote>
        <div class=""><br class="">
        </div>
        <div class="">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 class=""><br class="">
        </div>
        <div class="">- 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 class="">  Implementation might look something like this (<a
            moz-do-not-send="true"
            href="https://gist.github.com/karwa/5228974a0b4dfd000a916f0aac2721c6"
            class="">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 class=""><br class="">
        </div>
        <div class="">- We would add 3 new functions to all Collections:</div>
        <div class=""><br class="">
        </div>
      </div>
      <blockquote style="margin: 0 0 0 40px; border: none; padding:
        0px;" class="">
        <div class="">
          <div class=""><font class="" face="Courier">/// Repeats the
              collection <i class="">itself</i> N times.</font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier">///</font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier">func repeated(_
              times: Int) -&gt;
              RepeatCollection&lt;CollectionOfOne&lt;Self&gt;&gt;</font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier">/// Repeats the
              collection’s <i class="">contents</i> N times.</font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier">///</font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier">func
              repeatElements(_ times: Int) -&gt;
              RepeatCollection&lt;Self&gt;</font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier">/// Loops the
              collection’s contents to present a Collection of length N.</font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier">///</font></div>
        </div>
        <div class="">
          <div class=""><font class="" face="Courier">func
              repeatElements(count: Int) -&gt;
              RepeatCollection&lt;Self&gt;</font></div>
        </div>
      </blockquote>
      <div class="">
        <div class=""><br class="">
        </div>
        <div class="">- We would replace the existing Repeated&lt;T&gt;
          type with a typealias to
          RepeatCollection&lt;CollectionOfOne&lt;T&gt;&gt;</div>
        <div class="">- The existing, top-level repeatElement(T, Int)
          function <i class="">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 class=""><br class="">
        </div>
        <div class="">Example usage of the new API:</div>
        <div class=""><br class="">
        </div>
        <blockquote style="margin: 0 0 0 40px; border: none; padding:
          0px;" class="">
          <div class=""><font class="" face="Courier">// Equivalent to
              repeatElement(1, count: 3)</font></div>
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
          <div class=""><font class="" face="Courier">&gt; for i in
              CollectionOfOne(1).repeatElements(3).forEach { print(i) }</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
          <div class=""><font class="" face="Courier">// Equivalent to
              repeatElement([1, 2, 3], count: 3)</font></div>
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
          <div class="">
            <div class=""><font class="" face="Courier">&gt; for i in
                [1, 2, 3].repeated(3).forEach { print(i) }</font></div>
            <div class=""><font class="" face="Courier">[1, 2, 3]</font></div>
            <div class=""><span style="font-family: Courier;" class="">[1,
                2, 3]</span></div>
            <div class=""><span style="font-family: Courier;" class="">[1,
                2, 3]</span></div>
          </div>
          <div class=""><span style="font-family: Courier;" class=""><br
                class="">
            </span></div>
          <div class=""><span style="font-family: Courier;" class="">//
              New, flat repetition</span></div>
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
          <div class=""><font class="" face="Courier">&gt; for i in [1,
              2, 3].repeatElements(3) { print(i) }</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier">2</font></div>
          <div class=""><font class="" face="Courier">3</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier">2</font></div>
          <div class=""><font class="" face="Courier">3</font></div>
          <div class=""><font class="" face="Courier">1</font></div>
          <div class=""><font class="" face="Courier">2</font></div>
          <div class=""><font class="" face="Courier">3</font></div>
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
          <div class="">
            <div class=""><span style="font-family: Courier;" class="">//
                New, flat repetition</span></div>
            <div class=""><font class="" face="Courier"><br class="">
              </font></div>
            <div class=""><font class="" face="Courier">&gt; for i in
                [1, 2, 3].repeatElements(count: 4) { print(i) }</font></div>
            <div class=""><font class="" face="Courier">1</font></div>
            <div class=""><font class="" face="Courier">2</font></div>
            <div class=""><font class="" face="Courier">3</font></div>
            <div class=""><font class="" face="Courier">1</font></div>
            <div class=""><br class="">
            </div>
          </div>
          <div class=""><font class="" face="Courier">// Additional
              benefit: you can now repeat slices!</font></div>
          <div class=""><font class="" face="Courier"><br class="">
            </font></div>
          <div class=""><font class="" face="Courier">&gt;
              String(“yellow”.characters.dropFirst().dropLast().repeat(times:
              3))</font></div>
          <div class=""><font class="" face="Courier">“elloelloello"</font></div>
          <div class=""><br class="">
          </div>
          <div class=""><br class="">
          </div>
        </blockquote>
        Thoughts?</div>
      <div class=""><br class="">
      </div>
      <div class="">- Karl</div>
    </blockquote>
  </body>
</html>