Created
February 1, 2009 07:26
-
-
Save hakobe/55819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
package Count; | |
sub new { | |
my $class = shift; | |
my $start = shift || 0; | |
bless { count => $start }, $class; | |
} | |
sub next { | |
my $self = shift; | |
$self->{count}++; | |
} | |
package Fib; | |
sub new { | |
my $class = shift; | |
bless { seq => [0, 1], count => 0 }, $class; | |
} | |
sub next { | |
my $self = shift; | |
if (scalar @{$self->{seq}} <= $self->{count}) { | |
$self->generate; | |
} | |
$self->{seq}->[$self->{count}++]; | |
} | |
sub generate { | |
my $self = shift; | |
$self->{seq}->[ $self->{count} ] | |
= $self->{seq}->[ $self->{count} - 1 ] + $self->{seq}->[ $self->{count} - 2 ] | |
} | |
package main; | |
use Perl6::Say; | |
my $c = Count->new; | |
for (0..9) { | |
say $c->next; | |
} | |
say; | |
my $f = Fib->new; | |
for (0..9) { | |
say $f->next; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment