title: how to cycle through list indefinitely and lazily in Raku?
body: <p>This is mostly to geek out on how awesome <a href="https://docs.raku.org/" rel="nofollow noreferrer">Raku</a> is.</p>
  <p><strong>Question</strong></p>
  <p>Are there built-in methods that will take a list and cycle through it indefinitely, producing, say, the lazy list</p>
  <pre><code>a, b, c, a, b, c, ...
  </code></pre>
  <p>out of <code>(a, b, c)</code>? Nothing in the <a href="https://docs.raku.org/type/List" rel="nofollow noreferrer">documentation</a> on lists seems to obviously do the trick.</p>
  <p><strong>Possible solutions</strong></p>
  <p>I can think of at least a couple.</p>
  <p>The more plodding down-to-earth method would be to <a href="https://docs.raku.org/type/Map" rel="nofollow noreferrer">map</a> <code>@array[&lt;variable&gt; mod length-of-@array]</code> over the lazy range <code>0..Inf</code>. In the <code>perl6</code> REPL:</p>
  <pre><code>&gt; my @ar=&lt;a b c&gt;
  [a b c]
  &gt; (0..Inf).map({ @ar[$_ % @ar.elems] }).[0..100]
  (a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a ...)
  </code></pre>
  <p>A cooler (I think) solution would have you first turn your list into a <a href="https://docs.raku.org/language/operators#prefix_%7C" rel="nofollow noreferrer">slip</a>, and then apply the <a href="https://docs.raku.org/routine/xx" rel="nofollow noreferrer">repetition operator</a> to that slip indefinitely:</p>
  <pre><code>&gt; my @ar=&lt;a b c&gt;
  [a b c]
  &gt; (|@ar xx *).[0..100]
  (a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a ...)
  </code></pre>
  <p><strong>Conclusion</strong></p>
  <p>Even though I can achieve this (and the last solution in particular is very pithy and satisfying), what I am wondering is whether I'm missing anything built in specifically for this purpose.</p>
  <hr />
  <p><strong>Edit Re: the accepted answer</strong></p>
  <p>This is to elaborate slightly on the answer by @Brad Gilbert. The <code>«~»</code> operator wraps <a href="https://docs.raku.org/language/5to6-perlop#Additive_operators" rel="nofollow noreferrer">string concatenation</a> <code>~</code> in the <code>« »</code> <a href="https://docs.raku.org/language/operators#Hyper_operators" rel="nofollow noreferrer">hyper operator</a>, which results in applying the wrapped binary operator to elements selected sequentially from the two lists.</p>
  <p>So to achieve what I wanted (list-cycling to a desired length, like say 100), one would do</p>
  <pre><code>&lt;a b c&gt; &lt;&lt;~&gt;&gt; (&quot;&quot; xx 100)
  </code></pre>
  <p>This produces</p>
  <pre><code>(a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a)
  </code></pre>
  <p>(100 entries). It does have a couple of drawbacks though:</p>
  <ul>
  <li>it coerces the list entries to strings, due to the application of <code>~</code></li>
  <li>it doesn't actually produce a lazy infinite list:</li>
  </ul>
  <pre><code>&lt;a b c&gt; &lt;&lt;~&gt;&gt; (&quot;&quot; xx *)
  </code></pre>
  <p>returns</p>
  <pre><code>List on right side of hyperop of infix:&lt;~&gt; is known to be infinite
    in block &lt;unit&gt; at &lt;unknown file&gt; line 1
  </code></pre>