chromatic / modern_perl_book

Modern Perl: the book
http://www.modernperlbooks.com/
664 stars 187 forks source link

Fibonacci series closure example can be made more clear #224

Closed nbezzala closed 10 years ago

nbezzala commented 11 years ago

I used this code:

sub gen_fib {
        my @fibs = (0, 1);

        return sub {

                my $item = shift;

                if ( $item >= @fibs ) {
                        for my $calc (@fibs .. $item ) {
                                $fibs[$calc] = $fibs[$calc - 2] + $fibs[$calc - 1];
                        }
                }
                return $fibs[$item];
        }

}

say gen_fib(5);

And I got this as the result: CODE(0x3f9bf4)

nbezzala commented 11 years ago

Maybe it would help to add a couple of lines like this:

my $func = gen_fib();
say $func->(15);
chromatic commented 10 years ago

Fixed in f4f96f4. Thanks for the report!