Array-FIFO

 view release on metacpan or  search on metacpan

lib/Array/FIFO.pm  view on Meta::CPAN

package Array::FIFO;
$Array::FIFO::VERSION = '0.13';
use Moose;
use List::Util qw(sum0);

use namespace::autoclean;


=head1 NAME

Array::FIFO - A Simple limitable FIFO array, with sum and average methods

=head1 VERSION

version 0.13

=head1 SYNOPSIS

    my $ar = Array::FIFO->new( limit => 12 );
    $ar->add( 20 );
    $ar->add( 18 );
    $ar->add( 22 );

    say $ar->average;

=head1 DESCRIPTION

Array::FIFO is meant to be a simple limitable array, for storing data in a FIFO
manner; with an optional limit to how large the array can get.  When the limit is
reached, the oldest value is returned by C<add> when new values are added.

It's intent is for numeric values (i.e. current load of a system), but it should work 
for other data types if you're not in need of the calculation methods.

The C<sum> and C<average> methods return the current sum and average of the 
data as you would expect.  It does this on once, then caches the result until
the array changes.


=head1 METHODS

=head2 C<new>

=over 4

=item C<limit> (optional)

Numeric value of how large the array is allowed to get.  When it reaches 
limit, every item added causes the oldest item to be removed.

If no value is passed, there is no max size.


=back

=head2 C<add>

    $ar->add( 99 );

You can add any type of item to the array; if it's not a number it will be
treated as a value of 0 when when calculating sum() and average().

Returns the oldest element in the array.

=head2 C<remove>



( run in 0.691 second using v1.01-cache-2.11-cpan-39bf76dae61 )