Set::Associate
Essentially, this is a simple toolkit to map an infinite-many items to a
corresponding finite-many values, i.e: A nick coloring algorithm.
The most simple usage of this code gives out values from "items"
sequentially, and remembers seen values and persists them within the
scope of the program, i.e:
my $set = Set::Associate->new(
on_items_empty => Set::Associate::RefillItems->linear(
items => [qw( red blue yellow )],
),
);
sub color_nick {
my $nick = shift;
return colorize( $nick, $set->get_associated( $nick );
}
...
printf '<< %s >> %s', color_nick( $nick ), $message;
And this is extensible to use some sort of persisting allocation method
such as a hash
my $set = Set::Associate->new(
on_items_empty => Set::Associate::RefillItems->linear(
items => [qw( red blue yellow )],
),
on_new_key => Set::Associate::NewKey->hash_sha1,
);
sub color_nick {
my $nick = shift;
return colorize( $nick, $set->get_associated( $nick );
}
...
printf '<< %s >> %s', color_nick( $nick ), $message;
Alternatively, you could use 1 of 2 random forms:
# Can produce colour runs if you're unlucky
my $set = Set::Associate->new(
on_items_empty => Set::Associate::RefillItems->linear(
items => [qw( red blue yellow )],
),
on_new_key => Set::Associate::NewKey->random_pick,
);
# Will exhaust the colour variation before giving out the same colour
# twice
my $set = Set::Associate->new(
on_items_empty => Set::Associate::RefillItems->shuffle(
items => [qw( red blue yellow )],
),
);
INSTALLATION
This is a Perl module distribution. It should be installed with whichever
tool you use to manage your installation of Perl, e.g. any of
cpanm .
cpan .
cpanp -i .
Consult http://www.cpan.org/modules/INSTALL.html for further instruction.
Should you wish to install this module manually, the procedure is
perl Makefile.PL
make
make test
make install
COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Kent Fredric
<kentfredric@gmail.com>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.