Subject: | It looks like intersection has an issue with list flating also? ideas for sets. |
I could completely be wrong (no formal fp background), but I think that
intersection is trying to do the wrong thing?
[fp.intersection.t]
In quickly looking at it, it seems that your just, in effect, looking
for duplicates in the combined list? This would work if you could
assert that each set is already unique, but I didn't see that (it's
very possible though that I just missed something).
Also, because your working from either side, if your lists are not
evenly sized you will pick up the duplicates multiple times.
[fp.set.t]
Along the same lines I don't think that it would be that far of a
stretch to actually build defined sets. I've been tinkering with things
and I think that with the addition of a 'flat' function, you could
enforce the usage of hash/arrayrefs for sets. If your willing to turn
off misc warnings (for odd element hash refs) then this could allow for
the set notation ( ie {1,2,3} ) to be valid.
It's not ideal as it does not address lists or non-refs, but it
at least works and would allow for defined sets. Another idea would be
to have a set() that would take anything and wrap it up as a [], then
flat would unpack it as deeply as needed.
Any who, it's been really fun playing with this, now that moose has
completely changed the way that I've been thinking, now here comes fp
to shift things again. Thanks for keeping me on my toes.
--
benh~
Subject: | fp.intersection.t |
#!/usr/bin/perl
use strict;
use warnings;
use Test::Most qw{no_plan};
use fp;
eq_or_diff (
[ intersection(qw{a a a b},qw{b}) ],
[ qw{b} ],
);
eq_or_diff (
[ intersection(qw{a a a },qw{b}) ],
[ ],
);
__END__
not ok 1
# Failed test at /Users/benh/git/toys/fp.intersection.t line 8.
# +----+-----+----+----------+
# | Elt|Got | Elt|Expected |
# +----+-----+----+----------+
# * 0|a * | |
# * 1|a * | |
# | 2|b | 0|b |
# +----+-----+----+----------+
not ok 2
# Failed test at /Users/benh/git/toys/fp.intersection.t line 13.
# +----+--------+----+----------+
# | Elt|Got | Elt|Expected |
# +----+--------+----+----------+
# * 0|[ * 0|[] *
# * 1| 'a', * | |
# * 2| 'a' * | |
# * 3|] * | |
# +----+--------+----+----------+
Subject: | fp.set.t |
#!/usr/bin/perl
use strict;
use warnings;
use Test::Most qw{no_plan};
use fp;
sub flat {
map { ( ref $_ eq 'ARRAY') ? flat(@$_)
: ( ref $_ eq 'HASH') ? grep{ defined } flat(%$_)
: $_ ;
} @_;
}
sub i {
my ($s1,$s2) = @_;
return grep{ member($_,flat($s2)) } flat($s1);
};
sub set { [@_] };
{
no warnings qw{misc};
eq_or_diff(
i( {1,2,3,4}, {2,5,10} ),
2,
);
}
eq_or_diff(
[ i( {1,2,3,4}, {2,3} ) ],
[ 2,3 ],
);
my $s = set( qw{a b c d e f} );
eq_or_diff (
[ i( $s, {2,3} ) ],
[ ],
);
eq_or_diff (
[ i( set(qw{x y z}), set(qw{1 2 z}) ) ],
[ 'z' ],
);