FP::HashSet - set operations for hash tables
use FP::Equal 'is_equal'; use FP::HashSet; # ":all"; my $A = array_to_hashset ["a","b","c"]; my $B = array_to_hashset ["a","c","d"]; is_equal hashset_to_array(hashset_union($A,$B)), ["a","b","c","d"]; is_equal hashset_to_array(hashset_intersection($A,$B)), ["a","c"]; is_equal hashset_to_array(hashset_difference($A,$B)), ["b"]; ok not hashset_is_subset($B,$A); ok hashset_is_subset(+{b => 1},$A); is hashset_size($A), 3; ok not hashset_empty($A); ok hashset_empty(+{}); #hashset_keys_unsorted($A) # ("a","b","c") or in another sort order; # *keys* not values, hence always strings. is_equal [hashset_keys ($A)], [("a","b","c")]; # (always sorted) # a la diff tool: is_equal hashset_diff($A,$B), +{ b => "-", d => "+" }; # to treat a hashset as a function: my $f = hashset_to_predicate ($A); ok $f->("a"); # counting the number of recurrences of keys: my $C= array_to_countedhashset ["a", "c", "x", "c", "c", "a"]; is $C->{a}, 2; is $C->{c}, 3; is $C->{x}, 1;
Hashsets are hash tables that are expected to have keys representing the values unambiguously (FP::Array::array_to_hashset will just use the stringification).
Note that hashset_to_array will use the *values* of the hashes, not the keys.
This is alpha software! Read the status section in the package README or on the website.
To install FunctionalPerl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm FunctionalPerl
CPAN shell
perl -MCPAN -e shell install FunctionalPerl
For more information on module installation, please visit the detailed CPAN module installation guide.