The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

FP::HashSet - set operations for hash tables

SYNOPSIS

use FP::Equal 'is_equal';
use FP::HashSet; # ":all";

my $A = 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;

DESCRIPTION

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.

NOTE

This is alpha software! Read the status section in the package README or on the website.