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.85 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.84 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.83 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.82 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.81 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.80 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.79 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.78 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.77 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.76 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.75 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.74 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.73 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.72 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.71 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.70 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.69 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.68 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.67 tracks/perl5/exercises/custom-set/Example.pm
trackler-2.2.1.66 tracks/perl5/exercises/custom-set/Example.pm