Skip to content

Commit 5b07a0f

Browse files
committed
Moved code into separate subs and updated docs
1 parent 66d1dc4 commit 5b07a0f

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

lib/Algorithm/Odometer/Gray.pm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use strict;
1717
};
1818
sub new { ## no critic (RequireArgUnpacking)
1919
my $class = shift;
20+
return bless odometer_gray(@_), $class;
21+
}
22+
sub odometer_gray { ## no critic (RequireArgUnpacking)
2023
croak "no wheels specified" unless @_;
2124
my @w = @_;
2225
croak "all wheels must have at least two positions"
@@ -25,7 +28,7 @@ use strict;
2528
my @f = 0 .. @w;
2629
my @o = (1) x @w;
2730
my $done;
28-
return bless sub {
31+
return sub {
2932
if ($done) { @c = (0) x @w; @f = 0 .. @w; @o = (1) x @w; $done=0; return }
3033
my @cur = map {$w[$_][$c[$_]]} 0..$#w;
3134
if ($f[0]==@w) { $done=1 }
@@ -39,7 +42,7 @@ use strict;
3942
}
4043
}
4144
return wantarray ? @cur : join '', map {defined()?$_:''} @cur;
42-
}, $class;
45+
};
4346
}
4447
}
4548

lib/Algorithm/Odometer/Tiny.pm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ use strict;
1717
};
1818
sub new { ## no critic (RequireArgUnpacking)
1919
my $class = shift;
20+
return bless odometer(@_), $class;
21+
}
22+
sub odometer { ## no critic (RequireArgUnpacking)
2023
croak "no wheels specified" unless @_;
2124
my @w = map { [ 1, ref eq 'ARRAY' ? @$_ : $_ ] } @_;
2225
my $done;
23-
return bless sub {
26+
return sub {
2427
if ($done) { $done=0; return }
2528
my @cur = map {$$_[$$_[0]]} @w;
2629
for(my $i=$#w;$i>=0;$i--) {
@@ -29,7 +32,7 @@ use strict;
2932
$done=1 unless $i;
3033
}
3134
return wantarray ? @cur : join '', map {defined()?$_:''} @cur;
32-
}, $class;
35+
};
3336
}
3437
}
3538

@@ -74,6 +77,11 @@ operator in list context will return all of the (remaining) values in
7477
the sequence as strings. In scalar context, the iterator will return
7578
C<undef> once, and then start the sequence from the beginning.
7679
80+
This class is named C<::Tiny> because the code for the odometer fits
81+
on a single page, and if you look at the source, you'll see a
82+
C<sub odometer> that you can copy out of the source code if you wish
83+
(if you're not using L<Carp|Carp>, just replace C<croak> with C<die>).
84+
7785
=head2 Example
7886
7987
The following wheels:

0 commit comments

Comments
 (0)