`$limit` isn't an array index.

It's an argument that is passed on to the Iterator that tells it to stop
when it has produced that many values.

(Also it returns a `Seq`, not a `Positional` [fixed])

You can think of it as the following:

    'abc def'.words.head( $limit );

`words` does something more like this though:

    sub words ( Str:D $input, $limit = Inf --> Seq ) {
        gather {
            my $count = 0;
            my $prev-ws = -1;

            loop {
                # stop if $limit has been reached
                last if ++$count > $limit;  #        <------------------- $limit

                # index of whitespace
                my $next-ws = $input.index( ' ', $prev-ws + 1 );

                # run after each iteration
                NEXT $prev-ws = $next-ws;

                with $next-ws {  # index found a space

                    # skip two whitespaces in a row
                    next if $prev-ws+1 == $next-ws;

                    # give the next value
                    take substr( $input, $prev-ws+1, $next-ws - $prev-ws - 1 );

                } else { # index didn't find a space

                    # stop if the last character was a space
                    last if $prev-ws+1 >= $input.chars;

                    # give the last value
                    take substr( $input, $prev-ws+1 );

                    # end of sequence
                    last;
                }
            }
        }
    }

    say words('abc def ghi jkl mno pqr stu vwx yz', 3).perl;
    # ("abc", "def", "ghi").Seq
On Wed, Sep 26, 2018 at 1:57 AM Todd Chester <toddandma...@zoho.com> wrote:
>
> Hi All,
>
> Over on
>      https://docs.perl6.org/routine/words
> I see
>
>     multi method words(Str:D $input: $limit = Inf --> Positional)
>
> HOW IN THE WORLD did they convert `$limit = Inf` into an
> array index!?!?!
>
> https://docs.perl6.org/type/Num#Inf
> And `Inf` means a number larger than Perl can handle or Infinity.
> What does that have to do with ANYTHING!?
>
> Yours in confusion,
> -T
>
> And the worst part is that I know how to use "words" and use it
> frequently.  And the documentation is a nightmare to understand.
>
> Link to Positional:
> https://docs.perl6.org/type/Positional
>     Role for objects which support indexing them using
>     postcircumfix:«[ ]» (usually list-like objects).
>     Example types with Positional role include List,
>     Array, Range, and Buf.
>
> I like him using `Str:D $input:` rather than just `Str:D:`
> and it is more intuitive

Reply via email to