Sha256: c8945df9b30ca3a81d3364429f28c588f7d017d71601ee8e1ec025a05d9270a6

Contents?: true

Size: 1.32 KB

Versions: 323

Compression:

Stored size: 1.32 KB

Contents

package Example;

use strict;
use warnings;

sub new {
    my $class = shift;

    my %set;
    @set{ @_ } = ();

    bless \%set, $class;
}

sub add {
    my( $self, $new ) = @_;

    $self->{$new} = 1;
    return $self;
}

sub delete :method {
    my( $self, $member ) = @_;

    delete $self->{$member};
    return $self;
}

sub union {
    my( $self, $other ) = @_;

    return __PACKAGE__->new( keys %$self, keys %$other );
}

sub difference {
    my( $self, $other ) = @_;

    return __PACKAGE__->new( grep { ! $other->is_member($_) } keys %$self );
}


sub is_disjoint {
    my( $self, $other ) = @_;

    return $self->intersect( $other )->size() == 0;
}

sub empty {
    my $self = shift;

    %$self = ();
    return $self;
}

sub intersect {
    my( $self, $other ) = @_;

    return __PACKAGE__->new( grep { $self->is_member($_) } keys %$other );
}

sub is_member {
    my( $self, $member ) = @_;

    return exists $self->{$member};
}

sub size {
    my $self = shift;

    return scalar keys %$self;
}

sub is_subset {
    my( $self, $other ) = @_;

    return $self->intersect( $other )->size() == $other->size();
}

sub to_list {
    my $self = shift;

    return keys %$self;
}

sub is_equal {
    my( $self, $other ) = @_;

    return $self->intersect($other)->size() == $self->size() && $self->size() == $other->size();
}

1;



Version data entries

323 entries across 323 versions & 1 rubygems

Version Path
trackler-2.2.1.105 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.104 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.103 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.102 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.101 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.100 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.99 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.98 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.97 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.96 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.95 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.94 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.93 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.92 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.91 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.90 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.89 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.88 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.87 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.86 tracks/perl5/exercises/custom-set/Example.pm