Sha256: 0a38bb56094f6dd286169ce5559673c4fb61b5d4b3c6560dec3be4b503444d87

Contents?: true

Size: 553 Bytes

Versions: 73

Compression:

Stored size: 553 Bytes

Contents

package Strain;

use strict;
use warnings;

sub keep { [ _filter(@_, 1) ] }
sub discard { [ _filter(@_, 0) ] }

sub _filter {
    my ($list, $function, $is_keeping) = @_;

    die "ArgumentError: 2nd parameter must be a function reference" unless ref $function eq 'CODE';

    my @result;

    foreach my $element (@$list) {
        if ($is_keeping) {
            push @result, $element if $function->($element);
            next;
        }
        # discard
        push @result, $element unless $function->($element);
    }

    return @result;
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.118 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.117 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.116 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.115 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.114 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.113 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.111 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.110 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.109 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.108 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.107 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm
trackler-2.2.1.106 tracks/perl5/exercises/strain/.meta/solutions/Strain.pm