The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

FP::Hash

SYNOPSIS

    use FP::Equal 'is_equal';
    use FP::Hash;

    my $a = {a => 1, b => 2};
    my $b = hash_set($a, "b", 3);
    my $c = hash_delete($b, "a");
    if (my ($v) = hash_perhaps_ref ($c, "x")) {
       is_equal $v, "XXX";
    }
    is_equal hash_update($a, 'a', sub { $_[0]+10 }),
             +{ a => 11, b => 2 };
    is_equal hash_update($a, 'x', sub { [@_] }),
             +{ a => 1, b => 2, x => [] };

    # The function passed to hash_update is run in list context! Empty
    # list means, delete the item.
    my $e = hash_update $a, 'a', sub { () };
    is_equal $e, +{ b => 2 };

    is_equal $c, +{b => 3};
    is_equal $a, +{a => 1, b => 2};

    is_equal subhash({a => 10, b => 11, c => 12}, "a", "c"),
             +{a => 10, c => 12};

    # Curried hash lookup:
    is_equal hashkey("foo")->({foo=> 10, bar=> 20}), 10;
    use FP::Array_sort qw(array_sort on);
    use FP::Ops qw(real_cmp);
    is_equal array_sort([ {a=> 3, b=> "a"}, {a=> 2, b=> "b"} ],
                        on hashkey("a"), \&real_cmp),
             [ {a=> 2, b=> "b"}, {a=> 3, b=> "a"} ];

    # NOTE: `mesh` might be added to List::Util, too
    is_equal +{ mesh [qw(a b c)], [2,3,4] },
            { a=> 2, b=> 3, c=> 4 };
    is_equal ziphash([qw(a b c)], [2,3,4]),
            { a=> 2, b=> 3, c=> 4 };

DESCRIPTION

Provides pure functions on hash tables. Note though that hash table updates simply copy the whole hash table, thus you may easily get bad computational complexity. (If you really care about that, and not so much about interoperability with other Perl code, perhaps port a functional hash tables implementation (like the one used by Clojure)?)

NOTE

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