Algorithm-Combinatorics
view release on metacpan or search on metacpan
Combinatorics.pm view on Meta::CPAN
# This is a bit dirty by now, the objective is to be able to
# pass an initial sequence to the iterator and avoid a test
# in each iteration saying whether the sequence was already
# returned or not, since that might potentially be done a lot
# of times.
#
# The solution is to return an iterator that has a first sequence
# associated. The first time you call it that sequence is returned
# and the iterator rebless itself to become just a wrapped coderef.
#
# Note that the public contract is that responds to next(), no
# iterator class name is documented.
package Algorithm::Combinatorics::Iterator;
sub new {
my ($class, $coderef, $first_seq) = @_;
if (defined $first_seq) {
return bless [$coderef, $first_seq], $class;
} else {
return bless $coderef, 'Algorithm::Combinatorics::JustCoderef';
}
}
sub next {
my ($self) = @_;
$_[0] = $self->[0];
bless $_[0], 'Algorithm::Combinatorics::JustCoderef';
return $self->[1];
}
package Algorithm::Combinatorics::JustCoderef;
sub next {
my ($self) = @_;
return $self->();
}
t/Tester.pm view on Meta::CPAN
package Tester;
use strict;
use Test::More;
sub __new {
my ($class, $coderef, $comparator) = @_;
$comparator = \&Test::More::is_deeply if not $comparator;
bless { to_test => $coderef, comparator => $comparator }, $class;
}
# Here @rest is mean to be either (@data) or (@data, $k).
sub __test {
my ($self, $expected, @rest) = @_;
my @result = ();
my $iter = $self->{to_test}(@rest);
while (my $c = $iter->next) {
push @result, $c;
( run in 1.308 second using v1.01-cache-2.11-cpan-de7293f3b23 )