FP::Hash
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};
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)?)
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.