Sha256: 8d65da54b35c1efcd187be8162e97dfde945f141b51d6fb9bcbde6714dadf028

Contents?: true

Size: 1.32 KB

Versions: 73

Compression:

Stored size: 1.32 KB

Contents

package CustomSet;

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

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.179 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.178 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.177 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.176 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.175 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.174 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.173 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.172 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.171 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.170 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.169 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.167 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.166 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.165 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.164 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.163 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.162 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.161 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.160 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm