/usr/share/perl5/Number/Tolerant/Union.pm is in libnumber-tolerant-perl 1.708-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | use strict;
use warnings;
package Number::Tolerant::Union;
# ABSTRACT: unions of tolerance ranges
$Number::Tolerant::Union::VERSION = '1.708';
#pod =head1 SYNOPSIS
#pod
#pod use Number::Tolerant;
#pod
#pod my $range1 = tolerance(10 => to => 12);
#pod my $range2 = tolerance(14 => to => 16);
#pod
#pod my $union = $range1 | $range2;
#pod
#pod if ($11 == $union) { ... } # this will happen
#pod if ($12 == $union) { ... } # so will this
#pod
#pod if ($13 == $union) { ... } # nothing will happen here
#pod
#pod if ($14 == $union) { ... } # this will happen
#pod if ($15 == $union) { ... } # so will this
#pod
#pod =head1 DESCRIPTION
#pod
#pod Number::Tolerant::Union is used by L<Number::Tolerant> to represent the union
#pod of multiple tolerances. A subset of the same operators that function on a
#pod tolerance will function on a union of tolerances, as listed below.
#pod
#pod =head1 METHODS
#pod
#pod =head2 new
#pod
#pod my $union = Number::Tolerant::Union->new(@list_of_tolerances);
#pod
#pod There is a C<new> method on the Number::Tolerant::Union class, but unions are
#pod meant to be created with the C<|> operator on a Number::Tolerant tolerance.
#pod
#pod The arguments to C<new> are a list of numbers or tolerances to be unioned.
#pod
#pod Intersecting ranges are not converted into a single range, but this may change
#pod in the future. (For example, the union of "5 to 10" and "7 to 12" is not "5 to
#pod 12.")
#pod
#pod =cut
sub new {
my $class = shift;
bless { options => [ @_ ] } => $class;
}
#pod =head2 options
#pod
#pod This method will return a list of all the acceptable options for the union.
#pod
#pod =cut
sub options {
my $self = shift;
return @{$self->{options}};
}
#pod =head2 Overloading
#pod
#pod Tolerance unions overload a few operations, mostly comparisons.
#pod
#pod =over
#pod
#pod =item numification
#pod
#pod Unions numify to undef. If there's a better idea, I'd love to hear it.
#pod
#pod =item stringification
#pod
#pod A tolerance stringifies to a short description of itself. This is a set of the
#pod union's options, parentheses-enclosed and joined by the word "or"
#pod
#pod =item equality
#pod
#pod A number is equal to a union if it is equal to any of its options.
#pod
#pod =item comparison
#pod
#pod A number is greater than a union if it is greater than all its options.
#pod
#pod A number is less than a union if it is less than all its options.
#pod
#pod =item union intersection
#pod
#pod An intersection (C<&>) with a union is commutted across all options. In other
#pod words:
#pod
#pod (a | b | c) & d ==yields==> ((a & d) | (b & d) | (c & d))
#pod
#pod Options that have no intersection with the new element are dropped. The
#pod intersection of a constant number and a union yields that number, if the number
#pod was in the union's ranges and otherwise yields nothing.
#pod
#pod =back
#pod
#pod =cut
use overload
'0+' => sub { undef },
'""' => sub { join(' or ', map { "($_)" } $_[0]->options) },
'==' => sub { for ($_[0]->options) { return 1 if $_ == $_[1] } return 0 },
'!=' => sub { for ($_[0]->options) { return 0 if $_ == $_[1] } return 1 },
'>' =>
sub {
if ($_[2]) { for ($_[0]->options) { return 0 unless $_[1] > $_ } return 1 }
else { for ($_[0]->options) { return 0 unless $_[1] < $_ } return 1 }
},
'<' =>
sub {
if ($_[2]) { for ($_[0]->options) { return 0 unless $_[1] < $_ } return 1 }
else { for ($_[0]->options) { return 0 unless $_[1] > $_ } return 1 }
},
'<=>' =>
sub {
if ($_[2]) { $_[0] < $_[1] ? 1 : $_[0] > $_[1] ? -1 : 0 }
else { $_[0] > $_[1] ? 1 : $_[0] < $_[1] ? -1 : 0 }
},
'|' => sub { __PACKAGE__->new($_[0]->options,$_[1]); },
'&' => sub {
eval { $_[1]->isa('Number::Tolerant') }
? __PACKAGE__->new(map { $_ & $_[1] } $_[0]->options )
: $_[1] == $_[0]
? $_[1]
: ();
},
fallback => 1;
#pod =head1 TODO
#pod
#pod Who knows. Collapsing overlapping options, probably.
#pod
#pod =cut
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Number::Tolerant::Union - unions of tolerance ranges
=head1 VERSION
version 1.708
=head1 SYNOPSIS
use Number::Tolerant;
my $range1 = tolerance(10 => to => 12);
my $range2 = tolerance(14 => to => 16);
my $union = $range1 | $range2;
if ($11 == $union) { ... } # this will happen
if ($12 == $union) { ... } # so will this
if ($13 == $union) { ... } # nothing will happen here
if ($14 == $union) { ... } # this will happen
if ($15 == $union) { ... } # so will this
=head1 DESCRIPTION
Number::Tolerant::Union is used by L<Number::Tolerant> to represent the union
of multiple tolerances. A subset of the same operators that function on a
tolerance will function on a union of tolerances, as listed below.
=head1 METHODS
=head2 new
my $union = Number::Tolerant::Union->new(@list_of_tolerances);
There is a C<new> method on the Number::Tolerant::Union class, but unions are
meant to be created with the C<|> operator on a Number::Tolerant tolerance.
The arguments to C<new> are a list of numbers or tolerances to be unioned.
Intersecting ranges are not converted into a single range, but this may change
in the future. (For example, the union of "5 to 10" and "7 to 12" is not "5 to
12.")
=head2 options
This method will return a list of all the acceptable options for the union.
=head2 Overloading
Tolerance unions overload a few operations, mostly comparisons.
=over
=item numification
Unions numify to undef. If there's a better idea, I'd love to hear it.
=item stringification
A tolerance stringifies to a short description of itself. This is a set of the
union's options, parentheses-enclosed and joined by the word "or"
=item equality
A number is equal to a union if it is equal to any of its options.
=item comparison
A number is greater than a union if it is greater than all its options.
A number is less than a union if it is less than all its options.
=item union intersection
An intersection (C<&>) with a union is commutted across all options. In other
words:
(a | b | c) & d ==yields==> ((a & d) | (b & d) | (c & d))
Options that have no intersection with the new element are dropped. The
intersection of a constant number and a union yields that number, if the number
was in the union's ranges and otherwise yields nothing.
=back
=head1 TODO
Who knows. Collapsing overlapping options, probably.
=head1 AUTHOR
Ricardo Signes <rjbs@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2004 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|