I can explain why this error occurs. The sequence operator ... tries to invoke your closure, but since you didn't provide any elements to start with, it tries to invoke the closure without arguments, and fails.

The solution is probably to not use the sequence operator for this kind of thing.

Based on what you have written, I guess that this is what you want to achieve:

use v6; my @blocks := (0..*).map: -> $n { -> $x, $y { $x * $y * $n } }; say @blocks[2](2, 9);

It doesn't make much sense to me, but it is an infinite list of closures, each one having a higher $n than the previous one.

Update: An approach that is closer to your original is to use the list repetition operator:

sub gen($x, $y) { state $n = 0; $x*$y*$n++ } my @foo := &gen xx *;

But that probably doesn't do what you want. Since merely creating the elements of the infinite list doesn't invoke the subroutine, $n is incremented in the order that the closures are called, not in the order they appear inside the list. In the example above, $n is shared between all references to &gen, so it will always increase by one. If you write it as

my @foo := sub ($x, $y) { state $n = 0; $x*$y*$n++ } xx *;

instead, each closure gets a separate $n, so calling

say @foo[2](1, 1); say @foo[0](1, 1); say @foo[5](1, 1); say @foo[0](1, 1);

Produces

0 0 0 1

In reply to Re: Perl6 lazy list of code blocks by moritz
in thread Perl6 lazy list of code blocks by aes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.