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.119 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.118 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.117 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.116 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.115 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.114 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.113 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.111 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.110 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.109 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.108 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.107 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm
trackler-2.2.1.106 tracks/perl5/exercises/custom-set/.meta/solutions/CustomSet.pm