| 1 | # | ||
| 2 | # Copyright (c) 2019 Christian Jaeger, copying@christianjaeger.ch | ||
| 3 | # | ||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||
| 7 | # bundled with this file. | ||
| 8 | # | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | FP::Abstract::Equal - equality protocol | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | package FPEqualExample::Foo { | ||
| 17 | 6 | ▶ | sub new { my $class = shift; bless [@_], $class } |
|
Subroutine
new: called CC=1 SCAR=0.0
| |||
| 18 | 3 | ▶ | sub FP_Equal_equal { |
|
Subroutine
FP_Equal_equal: called CC=1 SCAR=0.0
| |||
| 19 | 3 | my ($a, $b) = @_; | |
| 20 | # If you know you've got numbers in here only: | ||
| 21 | 3 | $$a[0] == $$b[0] | |
| 22 | # For generic values, you would instead: | ||
| 23 | #use FP::Equal; | ||
| 24 | #equal($$a[0], $$b[0]) | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | 1 | ▶ | use FP::Equal qw(equal); use FP::List; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | |||
| 30 | ok equal( list(10,20,30)->map | ||
| 31 | (sub{ equal(FPEqualExample::Foo->new(20), | ||
| 32 | FPEqualExample::Foo->new($_[0])) }), | ||
| 33 | list('', 1, '')); | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | Objects implementing this protocol can be compared using the functions | ||
| 38 | from L<FP::Equal>, primarily C<equal>. | ||
| 39 | |||
| 40 | The C<equal> function forces promises before doing further comparisons | ||
| 41 | or passing them to the C<FP_Equal_equal> method (only the immediate | ||
| 42 | layer, not deeply). C<FP_Equal_equal> is only ever called with the two | ||
| 43 | arguments (self and one method argument) being references of, | ||
| 44 | currently, the same type (C<equal> handles the other cases internally) | ||
| 45 | (TODO: how to handle subtypes?). In better(?) words, C<FP_Equal_equal> | ||
| 46 | implementations can rely on the second argument supporting the same | ||
| 47 | operations that the first one does (TODO: even into the future once | ||
| 48 | accepting subtyping? This is *alpha*.) Likewise, C<FP_Equal_equal> is | ||
| 49 | not called if the arguments are both the same reference (in this case | ||
| 50 | C<equal> simply returns true). | ||
| 51 | |||
| 52 | =head1 TODO | ||
| 53 | |||
| 54 | Handle circular data structures. | ||
| 55 | |||
| 56 | =head1 SEE ALSO | ||
| 57 | |||
| 58 | L<FP::Equal> | ||
| 59 | |||
| 60 | =head1 NOTE | ||
| 61 | |||
| 62 | This is alpha software! Read the status section in the package README | ||
| 63 | or on the L<website|http://functional-perl.org/>. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | package FP::Abstract::Equal; | ||
| 68 | |||
| 69 | 61 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 70 | 61 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 71 | 61 | ▶ | use warnings FATAL => 'uninitialized'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 72 | |||
| 73 | 194 | ▶ | sub FP_Interface__method_names { ("FP_Equal_equal") } |
|
Subroutine
FP_Interface__method_names: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 74 | |||
| 75 | 61 | 1 | |