Skip to content

Instantly share code, notes, and snippets.

@hakobe
Created February 1, 2009 07:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakobe/55819 to your computer and use it in GitHub Desktop.
Save hakobe/55819 to your computer and use it in GitHub Desktop.
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