Collections::Set - Easy to use iterator with unique elements


Collections documentation Contained in the Collections distribution.

Index


Code Index:

NAME

Top

Collections::Set - Easy to use iterator with unique elements

VERSION

Top

Version 0.01

SYNOPSIS

Top

    use Collections::Set;

    my $col = Collections::Set->new;

    # Add new element
    $col->add('some string');
    $col->add('some string');

    # Size will return 1 (One unique element)
    print $col->size;

    # Remove element
    $col>remove('some string');

    # Size will return 0
    print $col->size;

INSTANCE METHODS

Top

add($element)

Add $element as part of receiver's elements, if the element already exists it does nothing

  $colection->add(1);

add_all($elements)

Add all $collection's element as part of receiver's elements, it adds elements that don't already exists in the collection

  $colection->add_all($another_collection);
  ... or ...
  $collection->add_all(1,2,3);
  ... or ...
  $colection->add_all([1,2,3]);

to_ordered()

Returns an Collections::Ordered with the same elements of this collection

   my $col = $collection->to_ordered;

AUTHOR

Top

Mariano Wahlmann, <dichoso at gmail.com>

BUGS

Top

Please report any bugs or feature requests to dichoso at gmail.com. I will be notified, and then you'll be notified of progress on your bug as I make changes.

COPYRIGHT & LICENSE

Top


Collections documentation Contained in the Collections distribution.
package Collections::Set;

use strict;
use warnings;
use base qw(Collections::Ordered);

our $VERSION = '0.01';

sub add {
    my $self    = shift;
    my $element = \(shift);
    if ( !$self->exists($$element)) {
        $self->SUPER::add($$element)
    }
    return $$element;
}

sub add_all {
    my $self = shift;
    my @elements = $self->_parameter_to_array(@_);
    foreach (@elements) {
        $self->add($_);
    }
}

sub to_ordered {
    my $self = shift;

    my $new = Collections::Ordered->new;
    $new->add_all($self);
    return $new;
}

1;

__END__