| 1 | package Set::Functional; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Exporter qw{import}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Set::Functional - set operations for functional programming | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Version 1.04 | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | our $VERSION = '1.04'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | our @EXPORT_OK = qw{ | |||||||||||||||||||||||||||
| 20 | setify setify_by | ||||||||||||||||||||||||||||
| 21 | cartesian | ||||||||||||||||||||||||||||
| 22 | difference difference_by | ||||||||||||||||||||||||||||
| 23 | disjoint disjoint_by | ||||||||||||||||||||||||||||
| 24 | distinct distinct_by | ||||||||||||||||||||||||||||
| 25 | intersection intersection_by | ||||||||||||||||||||||||||||
| 26 | symmetric_difference symmetric_difference_by | ||||||||||||||||||||||||||||
| 27 | union union_by | ||||||||||||||||||||||||||||
| 28 | is_disjoint is_disjoint_by | ||||||||||||||||||||||||||||
| 29 | is_equal is_equal_by | ||||||||||||||||||||||||||||
| 30 | is_equivalent is_equivalent_by | ||||||||||||||||||||||||||||
| 31 | is_pairwise_disjoint is_pairwise_disjoint_by | ||||||||||||||||||||||||||||
| 32 | is_proper_subset is_proper_subset_by | ||||||||||||||||||||||||||||
| 33 | is_proper_superset is_proper_superset_by | ||||||||||||||||||||||||||||
| 34 | is_subset is_subset_by | ||||||||||||||||||||||||||||
| 35 | is_superset is_superset_by | ||||||||||||||||||||||||||||
| 36 | }; | ||||||||||||||||||||||||||||
| 37 | 5 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | This module provides basic set operations for native lists. The primary goal | ||||||||||||||||||||||||||||
| 42 | is to take advantage of Perl's native functional programming capabilities | ||||||||||||||||||||||||||||
| 43 | while relying solely on Pure Perl constructs to perform the set operations as | ||||||||||||||||||||||||||||
| 44 | fast as possible. All of these techniques have been benchmarked against other | ||||||||||||||||||||||||||||
| 45 | common Perl idioms to determine the optimal solution. These benchmarks can | ||||||||||||||||||||||||||||
| 46 | be found in this package (shortly). | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Each function is provided in two forms. The first form always expects simple | ||||||||||||||||||||||||||||
| 49 | flat data structures of defined elements. The second form expects a BLOCK | ||||||||||||||||||||||||||||
| 50 | (refered to as a choice function) to evaluate each member of the list to a | ||||||||||||||||||||||||||||
| 51 | defined value to determine how the element is a set member. These can be | ||||||||||||||||||||||||||||
| 52 | identified by the suffix "_by". None of these functions check definedness | ||||||||||||||||||||||||||||
| 53 | inline so as to eliminate the costly O(n) operation. All functions have been | ||||||||||||||||||||||||||||
| 54 | prototyped to give them a native Perl-ish look and feel. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Example usage: | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | use Set::Functional ':all'; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Set Creation | ||||||||||||||||||||||||||||
| 61 | my @deduped_numbers = setify(1 .. 10, 2 .. 11); | ||||||||||||||||||||||||||||
| 62 | my @deduped_objects_by_name = setify_by { $_->{name} } ({name => 'fred'}, {name => 'bob'}, {name => 'fred'}); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Set Operation | ||||||||||||||||||||||||||||
| 65 | my @all_permutations = cartesian \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | my @only_arr1_elements = difference \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 68 | my @only_arr1_elements_by_name = difference_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | my @unique_per_set = disjoint \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 71 | my @unique_per_set_by_name = disjoint_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | my @unique_elements = distinct \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 74 | my @unique_elements_by_name = distinct_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | my @shared_elements = intersection \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 77 | my @shared_elements_by_name = intersection_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | my @odd_occuring_elements = symmetric_difference \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 80 | my @odd_occuring_elements_by_name = symmetric_difference_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | my @all_elements = union \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 83 | my @all_elements_by_name = union_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Set Predicates | ||||||||||||||||||||||||||||
| 86 | my $is_all_of_arr1_distinct_from_arr2 = is_disjoint \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 87 | my $is_all_of_arr1_distinct_from_arr2_by_name = is_disjoint_by { $_->{name} } \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | my $is_arr1_the_same_as_arr2 = is_equal \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 90 | my $is_arr1_the_same_as_arr2_by_name = is_equal_by { $_->{name} } \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $are_all_sets_mutually_unique = is_pairwise_disjoint \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 93 | my $are_all_sets_mutually_unique_by_name = is_pairwise_disjoint_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | my $is_all_of_arr1_in_arr2_but_not_the_same_as_arr2 = is_proper_subset \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 96 | my $is_all_of_arr1_in_arr2_but_not_the_same_as_arr2_by_name = is_proper_subset_by { $_->{name} } \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | my $is_all_of_arr1_in_arr2 = is_subset \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 99 | my $is_all_of_arr1_in_arr2_by_name = is_subset_by { $_->{name} } \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | my $is_all_of_arr2_in_arr1_but_not_the_same_as_arr1 = is_proper_superset \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 102 | my $is_all_of_arr2_in_arr1_but_not_the_same_as_arr1_by_name = is_proper_superset_by { $_->{name} } \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | my $is_all_of_arr2_in_arr1 = is_superset \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 105 | my $is_all_of_arr2_in_arr1_by_name = is_superset_by { $_->{name} } \@arr1, \@arr2; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 setify(@) | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Given a list, return a new set. Order is not guaranteed. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | setify 1 .. 10, 6 .. 15 => 1 .. 15 | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 4 | ▶ | sub setify(@) { | ||||||||||||||||||||||||||
|
Subroutine
setify: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 4 | my %set; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 4 | ▶ | undef @set{@_} if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 4 | return keys %set; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 setify_by(&@) | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Given a choice function and a list, return a new set defined by the choice | ||||||||||||||||||||||||||||
| 130 | function. Order is not guaranteed. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 4 | ▶ | sub setify_by(&@){ | ||||||||||||||||||||||||||
|
Subroutine
setify_by: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 4 | my $func = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 4 | my %set; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 4 | ▶ | @set{ map { $func->($_) } @_ } = @_ if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 4 | return values %set; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 OPERATORS | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 cartesian(@) | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Given multiple set references, return multiple sets containing all permutations | ||||||||||||||||||||||||||||
| 151 | of one element from each set. If the empty set is provided, no sets are | ||||||||||||||||||||||||||||
| 152 | returned since the number of sets generated should be the product of the input | ||||||||||||||||||||||||||||
| 153 | sets' cardinalities. If no sets are provided then none are returned. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | cartesian [1 .. 3], [1 .. 2] => [1,1],[1,2],[2,1],[2,2],[3,1],[3,2] | ||||||||||||||||||||||||||||
| 156 | cartesin => () | ||||||||||||||||||||||||||||
| 157 | cartesin [1 .. 3], [] => () | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 7 | ▶ | sub cartesian(@) { | ||||||||||||||||||||||||||
|
Subroutine
cartesian: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 7 | ▶ | return unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 6 | my @results; | |||||||||||||||||||||||||||
| 165 | 6 | my $repetitions = 1; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 6 | ▶ | ($repetitions *= @$_) || return for @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 4 | $#results = $repetitions - 1; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 4 | for my $idx (0 .. $#results) { | |||||||||||||||||||||||||||
| 171 | 2130 | $repetitions = @results; | |||||||||||||||||||||||||||
| 172 | 2130 | $results[$idx] = [map { $_->[int($idx/($repetitions /= @$_)) % @$_] } @_]; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 4 | return @results; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 difference(@) | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Given multiple set references, return a new set with all the elements in the first set | ||||||||||||||||||||||||||||
| 181 | that don't exist in subsequent sets. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | difference [1 .. 10], [6 .. 15] => 1 .. 5 | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 5 | ▶ | sub difference(@) { | ||||||||||||||||||||||||||
|
Subroutine
difference: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 5 | my $first = shift; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 5 | ▶ | return unless $first && @$first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$first and @$first
MC/DC50%1/2
$first and @$first
$first
@$first
Truth table66%2/3
A and B
A= $firstB= @$first
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 4 | my %set; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 4 | undef @set{@$first}; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 4 | ▶ | do { delete @set{@$_} if @$_ } for @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 4 | return keys %set; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head2 difference_by(&@) | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Given a choice function and multiple set references, return a new set with all the elements | ||||||||||||||||||||||||||||
| 204 | in the first set that don't exist in subsequent sets according to the choice function. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 5 | ▶ | sub difference_by(&@) { | ||||||||||||||||||||||||||
|
Subroutine
difference_by: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 209 | 5 | my $func = shift; | |||||||||||||||||||||||||||
| 210 | 5 | my $first = shift; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 5 | ▶ | return unless $first && @$first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$first and @$first
MC/DC50%1/2
$first and @$first
$first
@$first
Truth table66%2/3
A and B
A= $firstB= @$first
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 4 | my %set; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 4 | @set{ map { $func->($_) } @$first } = @$first; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 4 | ▶ | do { delete @set{ map { $func->($_) } @$_ } if @$_ } for @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 4 | return values %set; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head2 disjoint(@) | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Given multiple set references, return corresponding sets containing all the elements from | ||||||||||||||||||||||||||||
| 226 | the original set that exist in any set exactly once. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | disjoint [1 .. 10], [6 .. 15] => [1 .. 5], [11 .. 15] | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 10 | ▶ | sub disjoint(@) { | ||||||||||||||||||||||||||
|
Subroutine
disjoint: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 10 | my %element_to_count; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 10 | do { ++$element_to_count{$_} for @$_ } for @_; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 10 | return map { [grep { $element_to_count{$_} == 1 } @$_] } @_; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head2 disjoint_by(&@) | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Given a choice function and multiple set references, return corresponding sets containing | ||||||||||||||||||||||||||||
| 243 | all the elements from the original set that exist in any set exactly once | ||||||||||||||||||||||||||||
| 244 | according to the choice function. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 10 | ▶ | sub disjoint_by(&@) { | ||||||||||||||||||||||||||
|
Subroutine
disjoint_by: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 249 | 10 | my $func = shift; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 10 | my %key_to_count; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 10 | do { ++$key_to_count{$func->($_)} for @$_ } for @_; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 10 | return map { [grep { $key_to_count{$func->($_)} == 1 } @$_] } @_; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head2 distinct(@) | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | Given multiple set references, return a new set containing all the elements that exist | ||||||||||||||||||||||||||||
| 261 | in any set exactly once. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | distinct [1 .. 10], [6 .. 15] => 1 .. 5, 11 .. 15 | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =cut | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 5 | ▶ | sub distinct(@) { | ||||||||||||||||||||||||||
|
Subroutine
distinct: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 268 | 5 | my %element_to_count; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 5 | do { ++$element_to_count{$_} for @$_ } for @_; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 5 | return grep { $element_to_count{$_} == 1 } keys %element_to_count; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =head2 distinct_by(&@) | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Given a choice function and multiple set references, return a new set containing all the | ||||||||||||||||||||||||||||
| 278 | elements that exist in any set exactly once according to the choice function. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =cut | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 5 | ▶ | sub distinct_by(&@) { | ||||||||||||||||||||||||||
|
Subroutine
distinct_by: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 283 | 5 | my $func = shift; | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 5 | my %key_to_count; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 5 | for (@_) { | |||||||||||||||||||||||||||
| 288 | 9 | for (@$_) { | |||||||||||||||||||||||||||
| 289 | 80 | my $key = $func->($_); | |||||||||||||||||||||||||||
| 290 | 80 | ▶ | $key_to_count{$key} = exists $key_to_count{$key} ? undef : $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 5 | return grep { defined } values %key_to_count; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head2 intersection(@) | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | Given multiple set references, return a new set containing all the elements that exist | ||||||||||||||||||||||||||||
| 300 | in all sets. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | intersection [1 .. 10], [6 .. 15] => 6 .. 10 | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =cut | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 53 | ▶ | sub intersection(@) { | ||||||||||||||||||||||||||
|
Subroutine
intersection: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 307 | 53 | my $first = shift; | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 53 | ▶ | return unless $first && @$first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first and @$first
MC/DC100%2/2
$first and @$first
$first
@$first
Truth table100%3/3
A and B
A= $firstB= @$first
| |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 40 | my %set; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 40 | undef @set{@$first}; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 40 | for (@_) { | |||||||||||||||||||||||||||
| 316 | 41 | my @int = grep { exists $set{$_} } @$_; | |||||||||||||||||||||||||||
| 317 | 41 | ▶ | return unless @int; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 28 | %set = (); | |||||||||||||||||||||||||||
| 319 | 28 | undef @set{@int}; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 27 | return keys %set; | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | =head2 intersection_by(&@) | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | Given a choice function and multiple set references, return a new set containing all the | ||||||||||||||||||||||||||||
| 328 | elements that exist in all sets according to the choice function. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =cut | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 53 | ▶ | sub intersection_by(&@) { | ||||||||||||||||||||||||||
|
Subroutine
intersection_by: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 333 | 53 | my $func = shift; | |||||||||||||||||||||||||||
| 334 | 53 | my $first = shift; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 53 | ▶ | return unless $first && @$first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first and @$first
MC/DC100%2/2
$first and @$first
$first
@$first
Truth table100%3/3
A and B
A= $firstB= @$first
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 40 | my %set; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 40 | @set{ map { $func->($_) } @$first } = @$first; | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 40 | for (@_) { | |||||||||||||||||||||||||||
| 343 | 41 | my @int = grep { exists $set{$func->($_)} } @$_; | |||||||||||||||||||||||||||
| 344 | 41 | ▶ | return unless @int; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 28 | %set = (); | |||||||||||||||||||||||||||
| 346 | 28 | @set{ map { $func->($_) } @int } = @int; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 27 | return values %set; | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head2 symmetric_difference(@) | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Given multiple set references, return a new set containing all the elements that | ||||||||||||||||||||||||||||
| 355 | exist an odd number of times across all sets. | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | symmetric_difference [1 .. 10], [6 .. 15], [4, 8, 12] => 1 .. 5, 8, 11 .. 15 | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =cut | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 5 | ▶ | sub symmetric_difference(@) { | ||||||||||||||||||||||||||
|
Subroutine
symmetric_difference: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 362 | 5 | my $count; | |||||||||||||||||||||||||||
| 363 | my %element_to_count; | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 5 | do { ++$element_to_count{$_} for @$_ } for @_; | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 5 | return grep { $element_to_count{$_} % 2 } keys %element_to_count; | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =head2 symmetric_difference_by(&@) | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | Given a choice function and multiple set references, return a new set containing | ||||||||||||||||||||||||||||
| 373 | all the elements that exist an odd number of times across all sets according to | ||||||||||||||||||||||||||||
| 374 | the choice function. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =cut | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 5 | ▶ | sub symmetric_difference_by(&@) { | ||||||||||||||||||||||||||
|
Subroutine
symmetric_difference_by: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 379 | 5 | my $func = shift; | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 5 | my $count; | |||||||||||||||||||||||||||
| 382 | my %key_to_count; | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 5 | do { ++$key_to_count{$func->($_)} for @$_ } for @_; | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | return map { | ||||||||||||||||||||||||||||
| 387 | 5 | grep { | |||||||||||||||||||||||||||
| 388 | 9 | $count = delete $key_to_count{$func->($_)}; | |||||||||||||||||||||||||||
| 389 | 80 | ▶ | defined($count) && $count % 2 | ||||||||||||||||||||||||||
|
Condition100%3/3
defined $count && $count % 2
MC/DC100%2/2
defined $count && $count % 2
defined $count
$count % 2
Truth table100%3/3
A && B
A= defined $countB= $count % 2
| |||||||||||||||||||||||||||||
| 390 | } @$_ | ||||||||||||||||||||||||||||
| 391 | } @_; | ||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =head2 union(@) | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Given multiple set references, return a new set containing all the elements that exist | ||||||||||||||||||||||||||||
| 397 | in any set. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | union [1 .. 10], [6 .. 15] => 1 .. 15 | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =cut | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 5 | ▶ | sub union(@) { | ||||||||||||||||||||||||||
|
Subroutine
union: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 404 | 5 | my %set; | |||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 5 | ▶ | do { undef @set{@$_} if @$_ } for @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 5 | return keys %set; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head2 union_by(&@) | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | Given a choice function and multiple set references, return a new set containing all the | ||||||||||||||||||||||||||||
| 414 | elements that exist in any set according to the choice function. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =cut | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 5 | ▶ | sub union_by(&@) { | ||||||||||||||||||||||||||
|
Subroutine
union_by: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 419 | 5 | my $func = shift; | |||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 5 | my %set; | |||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 5 | ▶ | do { @set{ map { $func->($_) } @$_ } = @$_ if @$_ } for @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 5 | return values %set; | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =head1 PREDICATES | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =cut | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =head2 is_disjoint($$) | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Given two set references, return true if both sets contain none of the same values. | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | is_disjoint [1 .. 5], [6 .. 10] => true | ||||||||||||||||||||||||||||
| 437 | is_disjoint [1 .. 6], [4 .. 10] => false | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =cut | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 8 | ▶ | sub is_disjoint($$) { | ||||||||||||||||||||||||||
|
Subroutine
is_disjoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 442 | 8 | my @set = &intersection(@_[0,1]); | |||||||||||||||||||||||||||
| 443 | 8 | return ! @set; | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =head2 is_disjoint_by(&$$) | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | Given a choice function and two sets references, return true if both sets | ||||||||||||||||||||||||||||
| 449 | contain none of the same values according to the choice function. | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =cut | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 8 | ▶ | sub is_disjoint_by(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
is_disjoint_by: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 454 | 8 | my @set = &intersection_by(@_[0,1,2]); | |||||||||||||||||||||||||||
| 455 | 8 | return ! @set; | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =head2 is_equal($$) | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | Given two set references, return true if both sets contain all the same values. | ||||||||||||||||||||||||||||
| 461 | Aliased by is_equivalent. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | is_equal [1 .. 5], [1 .. 5] => true | ||||||||||||||||||||||||||||
| 464 | is_equal [1 .. 10], [6 .. 15] => false | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | =cut | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 8 | ▶ | sub is_equal($$) { | ||||||||||||||||||||||||||
|
Subroutine
is_equal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 469 | 8 | my @set = &intersection(@_[0,1]); | |||||||||||||||||||||||||||
| 470 | 8 | ▶ | return @set == @{$_[0]} && @set == @{$_[1]}; | ||||||||||||||||||||||||||
|
Condition100%3/3
@set == @{$_[0];} && @set == @{$_[1];}
MC/DC100%2/2
@set == @{$_[0];} && @set == @{$_[1];}
@set == @{$_[0];}
@set == @{$_[1];}
Truth table100%3/3
A && B
A= @set == @{$_[0];}B= @set == @{$_[1];}
| |||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | 5 | *is_equivalent = \&is_equal; | |||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =head2 is_equal_by(&$$) | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | Given a choice function and two sets references, return true if both sets | ||||||||||||||||||||||||||||
| 477 | contain all the same values according to the choice function. | ||||||||||||||||||||||||||||
| 478 | Aliased by is_equivalent_by. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =cut | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 8 | ▶ | sub is_equal_by(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
is_equal_by: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 483 | 8 | my @set = &intersection_by(@_[0,1,2]); | |||||||||||||||||||||||||||
| 484 | 8 | ▶ | return @set == @{$_[1]} && @set == @{$_[2]}; | ||||||||||||||||||||||||||
|
Condition100%3/3
@set == @{$_[1];} && @set == @{$_[2];}
MC/DC100%2/2
@set == @{$_[1];} && @set == @{$_[2];}
@set == @{$_[1];}
@set == @{$_[2];}
Truth table100%3/3
A && B
A= @set == @{$_[1];}B= @set == @{$_[2];}
| |||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | 5 | *is_equivalent_by = \&is_equal_by; | |||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =head2 is_pairwise_disjoint(@) | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | Given multiple set references, return true if every set is disjoint from every | ||||||||||||||||||||||||||||
| 491 | other set. | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | is_pairwise_disjoint [1 .. 5], [6 .. 10], [11 .. 15] => true | ||||||||||||||||||||||||||||
| 494 | is_pairwise_disjoint [1 .. 5], [6 .. 10], [11 .. 15], [3 .. 8] => false | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | =cut | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 5 | ▶ | sub is_pairwise_disjoint(@) { | ||||||||||||||||||||||||||
|
Subroutine
is_pairwise_disjoint: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 499 | 5 | my @sets = &disjoint(@_); | |||||||||||||||||||||||||||
| 500 | 5 | ▶ | do { return 0 if @{$sets[$_]} != @{$_[$_]} } for 0 .. $#sets; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | 3 | return 1; | |||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | =head2 is_pairwise_disjoint_by(&@) | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | Given a choice function and multiple set references, return true if every set | ||||||||||||||||||||||||||||
| 507 | is disjoint from every other set according to the choice function. | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =cut | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 5 | ▶ | sub is_pairwise_disjoint_by(&@) { | ||||||||||||||||||||||||||
|
Subroutine
is_pairwise_disjoint_by: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 512 | 5 | my @sets = &disjoint_by((shift), @_); | |||||||||||||||||||||||||||
| 513 | 5 | ▶ | do { return 0 if @{$sets[$_]} != @{$_[$_]} } for 0 .. $#sets; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | 3 | return 1; | |||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | =head2 is_proper_subset($$) | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | Given two set references, return true if the first set is fully contained by | ||||||||||||||||||||||||||||
| 520 | but is not equivalent to the second. | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | is_proper_subset [1 .. 5], [1 .. 10] => true | ||||||||||||||||||||||||||||
| 523 | is_proper_subset [1 .. 5], [1 .. 5] => false | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =cut | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | 8 | ▶ | sub is_proper_subset($$) { | ||||||||||||||||||||||||||
|
Subroutine
is_proper_subset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 528 | 8 | my @set = &intersection(@_[0,1]); | |||||||||||||||||||||||||||
| 529 | 8 | ▶ | return @set == @{$_[0]} && @set != @{$_[1]}; | ||||||||||||||||||||||||||
|
Condition100%3/3
@set == @{$_[0];} && @set != @{$_[1];}
MC/DC100%2/2
@set == @{$_[0];} && @set != @{$_[1];}
@set == @{$_[0];}
@set != @{$_[1];}
Truth table100%3/3
A && B
A= @set == @{$_[0];}B= @set != @{$_[1];}
| |||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =head2 is_proper_subset_by(&$$) | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | Given a choice function and two set references, return true if the first set | ||||||||||||||||||||||||||||
| 535 | is fully contained by but is not equivalent to the second according to the | ||||||||||||||||||||||||||||
| 536 | choice function. | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | =cut | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | 8 | ▶ | sub is_proper_subset_by(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
is_proper_subset_by: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 541 | 8 | my @set = &intersection_by(@_[0,1,2]); | |||||||||||||||||||||||||||
| 542 | 8 | ▶ | return @set == @{$_[1]} && @set != @{$_[2]}; | ||||||||||||||||||||||||||
|
Condition100%3/3
@set == @{$_[1];} && @set != @{$_[2];}
MC/DC100%2/2
@set == @{$_[1];} && @set != @{$_[2];}
@set == @{$_[1];}
@set != @{$_[2];}
Truth table100%3/3
A && B
A= @set == @{$_[1];}B= @set != @{$_[2];}
| |||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | =head2 is_proper_superset($$) | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | Given two set references, return true if the first set fully contains but is | ||||||||||||||||||||||||||||
| 548 | not equivalent to the second. | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | is_proper_superset [1 .. 10], [1 .. 5] => true | ||||||||||||||||||||||||||||
| 551 | is_proper_superset [1 .. 5], [1 .. 5] => false | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | =cut | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 8 | ▶ | sub is_proper_superset($$) { | ||||||||||||||||||||||||||
|
Subroutine
is_proper_superset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 556 | 8 | my @set = &intersection(@_[0,1]); | |||||||||||||||||||||||||||
| 557 | 8 | ▶ | return @set != @{$_[0]} && @set == @{$_[1]}; | ||||||||||||||||||||||||||
|
Condition100%3/3
@set != @{$_[0];} && @set == @{$_[1];}
MC/DC100%2/2
@set != @{$_[0];} && @set == @{$_[1];}
@set != @{$_[0];}
@set == @{$_[1];}
Truth table100%3/3
A && B
A= @set != @{$_[0];}B= @set == @{$_[1];}
| |||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | =head2 is_proper_superset_by(&$$) | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | Given a choice function and two set references, return true if the first set | ||||||||||||||||||||||||||||
| 563 | fully contains but is not equivalent to the second according to the choice | ||||||||||||||||||||||||||||
| 564 | function. | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | =cut | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | 8 | ▶ | sub is_proper_superset_by(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
is_proper_superset_by: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 569 | 8 | my @set = &intersection_by(@_[0,1,2]); | |||||||||||||||||||||||||||
| 570 | 8 | ▶ | return @set != @{$_[1]} && @set == @{$_[2]}; | ||||||||||||||||||||||||||
|
Condition100%3/3
@set != @{$_[1];} && @set == @{$_[2];}
MC/DC100%2/2
@set != @{$_[1];} && @set == @{$_[2];}
@set != @{$_[1];}
@set == @{$_[2];}
Truth table100%3/3
A && B
A= @set != @{$_[1];}B= @set == @{$_[2];}
| |||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | =head2 is_subset($$) | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | Given two set references, return true if the first set is fully contained by | ||||||||||||||||||||||||||||
| 576 | the second. | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | is_subset [1 .. 5], [1 .. 10] => true | ||||||||||||||||||||||||||||
| 579 | is_subset [1 .. 5], [1 .. 5] => true | ||||||||||||||||||||||||||||
| 580 | is_subset [1 .. 5], [2 .. 11] => false | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | =cut | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 8 | ▶ | sub is_subset($$) { | ||||||||||||||||||||||||||
|
Subroutine
is_subset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 585 | 8 | my @set = &intersection(@_[0,1]); | |||||||||||||||||||||||||||
| 586 | 8 | return @set == @{$_[0]}; | |||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | =head2 is_subset_by(&$$) | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | Given a choice function and two set references, return true if the first set | ||||||||||||||||||||||||||||
| 592 | is fully contained by the second according to the choice function. | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | =cut | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | 8 | ▶ | sub is_subset_by(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
is_subset_by: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 597 | 8 | my @set = &intersection_by(@_[0,1,2]); | |||||||||||||||||||||||||||
| 598 | 8 | return @set == @{$_[1]}; | |||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | =head2 is_superset($$) | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | Given two set references, return true if the first set fully contains the | ||||||||||||||||||||||||||||
| 604 | second. | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | is_superset [1 .. 10], [1 .. 5] => true | ||||||||||||||||||||||||||||
| 607 | is_superset [1 .. 5], [1 .. 5] => true | ||||||||||||||||||||||||||||
| 608 | is_subset [1 .. 5], [2 .. 11] => false | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | =cut | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | 8 | ▶ | sub is_superset($$) { | ||||||||||||||||||||||||||
|
Subroutine
is_superset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 613 | 8 | my @set = &intersection(@_[0,1]); | |||||||||||||||||||||||||||
| 614 | 8 | return @set == @{$_[1]}; | |||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | =head2 is_superset_by(&$$) | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | Given a choice function and two set references, return true if the first set | ||||||||||||||||||||||||||||
| 620 | fully contains the second according to the choice function. | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | =cut | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | 8 | ▶ | sub is_superset_by(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
is_superset_by: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 625 | 8 | my @set = &intersection_by(@_[0,1,2]); | |||||||||||||||||||||||||||
| 626 | 8 | return @set == @{$_[2]}; | |||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | Aaron Cohen, C<< <aarondcohen at gmail.com> >> | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | Special thanks to: | ||||||||||||||||||||||||||||
| 634 | L<Logan Bell|http://metacpan.org/author/logie> | ||||||||||||||||||||||||||||
| 635 | L<Thomas Whaples|https://github.com/twhaples> | ||||||||||||||||||||||||||||
| 636 | L<Dibin Pookombil|https://github.com/dibinp> | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | =head1 BUGS | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | Please report any bugs or feature requests to C<bug-set-functional at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 641 | the web interface at L<https://github.com/aarondcohen/Set-Functional/issues>. I will | ||||||||||||||||||||||||||||
| 642 | be notified, and then you'll automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | =head1 TODO | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | =over 4 | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | =item * Add SEE ALSO section | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | =back | ||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||
| 652 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | perldoc Set::Functional | ||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||
| 658 | You can also look for information at: | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | =over 4 | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | =item * Official GitHub Repo | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | L<https://github.com/aarondcohen/Set-Functional> | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | =item * GitHub's Issue Tracker (report bugs here) | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | L<https://github.com/aarondcohen/Set-Functional/issues> | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | L<http://cpanratings.perl.org/d/Set-Functional> | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | =item * Official CPAN Page | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | L<http://search.cpan.org/dist/Set-Functional/> | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | =back | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | Copyright 2011-2014 Aaron Cohen. | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 685 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 686 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | =cut | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | 5 | 1; # End of Set::Functional | |||||||||||||||||||||||||||