Series Progress

After being stumped by it for a week, tonight I suddenly realized how to I could get the series operator working for the general cases. The trick was to keep an array storing the last N values generated in the series, where N is the number of arguments that the generator function takes. There was a good solid hour of banging my head against the compiler, but finally I got this:

> (1, 1, { $^a + $^b } ... *).batch(20).perl.say
(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765)

Here's the key new code:

    my $arity = any( $next.signature.params>>.slurpy ) ?? Inf !! $next.count;

    gather {
        my @args;
        my $j;
        my $top = $arity min @lhs.elems;
        for 0..^$top -> $i {
            $j = @lhs[$i];
            take $j;
            @args.push($j);
        }

        if !$limit.defined || $limit cmp $j != 0 {
            loop {
                my $i = $next.(|@args);
                my $j = $i;
 
                my $cur_cmp = 1;
                if $limit.defined {
                    $cur_cmp = $limit cmp $j;
                    last if (@args[@args.elems - 1] cmp $limit) == $cur_cmp;
                }
                take $j;
                last if $cur_cmp == 0;

                @args.push($j);
                while @args.elems > $arity {
                    @args.shift;
                }
            }
        }
    }

I'm sure there are still some bugs hiding in there, and I need to throw a bunch more tests at it, but it seems like a fair evening's work. I would love to get series test suggestions from people, and if you'd like to play with what I've got now, you can grab Rakudo's "series" branch and build it locally.

About these ads

One Response to “Series Progress”

  1. colomon Says:

    I should have said, tests are in S03-operators/series-*.t. :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: