| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2014-2022 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | FP::Hash | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use FP::Equal 'is_equal'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use FP::Hash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | my $a = {a => 1, b => 2}; | ||||||||||||||||||||||||||||
| 20 | my $b = hash_set($a, "b", 3); | ||||||||||||||||||||||||||||
| 21 | my $c = hash_delete($b, "a"); | ||||||||||||||||||||||||||||
| 22 | if (my ($v) = hash_perhaps_ref ($c, "x")) { | ||||||||||||||||||||||||||||
| 23 | is_equal $v, "XXX"; | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | is_equal hash_update($a, 'a', sub { $_[0]+10 }), | ||||||||||||||||||||||||||||
| 26 | +{ a => 11, b => 2 }; | ||||||||||||||||||||||||||||
| 27 | is_equal hash_update($a, 'x', sub { [@_] }), | ||||||||||||||||||||||||||||
| 28 | +{ a => 1, b => 2, x => [] }; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # The function passed to hash_update is run in list context! Empty | ||||||||||||||||||||||||||||
| 31 | # list means, delete the item. | ||||||||||||||||||||||||||||
| 32 | my $e = hash_update $a, 'a', sub { () }; | ||||||||||||||||||||||||||||
| 33 | is_equal $e, +{ b => 2 }; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | is_equal $c, +{b => 3}; | ||||||||||||||||||||||||||||
| 36 | is_equal $a, +{a => 1, b => 2}; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | is_equal subhash({a => 10, b => 11, c => 12}, "a", "c"), | ||||||||||||||||||||||||||||
| 39 | +{a => 10, c => 12}; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Curried hash lookup: | ||||||||||||||||||||||||||||
| 42 | is_equal hashkey("foo")->({foo=> 10, bar=> 20}), 10; | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use FP::Array_sort qw(array_sort on); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | use FP::Ops qw(real_cmp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | is_equal array_sort([ {a=> 3, b=> "a"}, {a=> 2, b=> "b"} ], | ||||||||||||||||||||||||||||
| 46 | on hashkey("a"), \&real_cmp), | ||||||||||||||||||||||||||||
| 47 | [ {a=> 2, b=> "b"}, {a=> 3, b=> "a"} ]; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | is_equal hash_map({a=> 1, b=> 2, c=> 33, d=> 4}, | ||||||||||||||||||||||||||||
| 50 | sub { | ||||||||||||||||||||||||||||
| 51 | my ($k, $v)= @_; | ||||||||||||||||||||||||||||
| 52 | $v > 10 ? () : (uc $k, $v*2) | ||||||||||||||||||||||||||||
| 53 | }), | ||||||||||||||||||||||||||||
| 54 | {A=> 2, B=> 4, D=> 8}; | ||||||||||||||||||||||||||||
| 55 | is_equal hash_filter({a=> 1, b=> 2, c=> 33, d=> 4}, | ||||||||||||||||||||||||||||
| 56 | sub { | ||||||||||||||||||||||||||||
| 57 | my ($k, $v)= @_; | ||||||||||||||||||||||||||||
| 58 | $v >= 2 and $k lt 'd' | ||||||||||||||||||||||||||||
| 59 | }), | ||||||||||||||||||||||||||||
| 60 | {b=> 2, c=> 33}; | ||||||||||||||||||||||||||||
| 61 | is_equal hash_key_filter({a=> 1, b=> 2, c=> 33, d=> 4}, | ||||||||||||||||||||||||||||
| 62 | sub { | ||||||||||||||||||||||||||||
| 63 | my ($k)= @_; | ||||||||||||||||||||||||||||
| 64 | $k lt 'b' | ||||||||||||||||||||||||||||
| 65 | }), | ||||||||||||||||||||||||||||
| 66 | {a=> 1}; | ||||||||||||||||||||||||||||
| 67 | is_equal hash_value_filter({a=> 1, b=> 2, c=> 33, d=> 4}, | ||||||||||||||||||||||||||||
| 68 | sub { | ||||||||||||||||||||||||||||
| 69 | my ($v)= @_; | ||||||||||||||||||||||||||||
| 70 | $v <= 4 | ||||||||||||||||||||||||||||
| 71 | }), | ||||||||||||||||||||||||||||
| 72 | {a=> 1, b=> 2, d=> 4}; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # NOTE: `mesh` might be added to List::Util, too | ||||||||||||||||||||||||||||
| 75 | is_equal +{ mesh [qw(a b c)], [2,3,4] }, | ||||||||||||||||||||||||||||
| 76 | { a=> 2, b=> 3, c=> 4 }; | ||||||||||||||||||||||||||||
| 77 | is_equal ziphash([qw(a b c)], [2,3,4]), | ||||||||||||||||||||||||||||
| 78 | { a=> 2, b=> 3, c=> 4 }; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Provides pure functions on hash tables. Note though that hash table | ||||||||||||||||||||||||||||
| 83 | updates simply copy the whole hash table, thus you may easily get bad | ||||||||||||||||||||||||||||
| 84 | computational complexity. (If you really care about that, and not so | ||||||||||||||||||||||||||||
| 85 | much about interoperability with other Perl code, perhaps port a | ||||||||||||||||||||||||||||
| 86 | functional hash tables implementation (like the one used by Clojure)?) | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 NOTE | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||
| 92 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | package FP::Hash; | ||||||||||||||||||||||||||||
| 97 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 12 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 100 | 12 | ▶ | use Exporter "import"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # XX should not export all of those by default? | ||||||||||||||||||||||||||||
| 103 | 12 | our @EXPORT = qw(hash_set hash_perhaps_ref hash_maybe_ref hash_xref | |||||||||||||||||||||||||||
| 104 | hash_ref_or hashkey mesh ziphash hash_cache hash_delete | ||||||||||||||||||||||||||||
| 105 | hash_update hash_diff hash_length subhash hashes_keys $empty_hash | ||||||||||||||||||||||||||||
| 106 | hash_map hash_filter hash_key_filter hash_value_filter | ||||||||||||||||||||||||||||
| 107 | hash2_set | ||||||||||||||||||||||||||||
| 108 | hash_to_maybefunction hash_to_perhapsfunction hash_to_function | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | 12 | our @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 111 | 12 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 12 | ▶ | use FP::Docstring; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 12 | ▶ | use Chj::TEST; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 12 | ▶ | use FP::Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | #use Const::Fast; | ||||||||||||||||||||||||||||
| 118 | #const my %empty_hash; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # since Const::Fast doesn't (currently) buy anything more than | ||||||||||||||||||||||||||||
| 121 | # Hash::Util's lock_hash, use the latter for less dependencies. | ||||||||||||||||||||||||||||
| 122 | 12 | ▶ | use Hash::Util 'lock_hash'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | 12 | my %empty_hash; | |||||||||||||||||||||||||||
| 124 | 12 | lock_hash %empty_hash; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 12 | our $empty_hash = \%empty_hash; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | #TEST_EXCEPTION { $$empty_hash{a} } | ||||||||||||||||||||||||||||
| 129 | # undef; | ||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||
| 131 | # Aw, that also gives 'Attempt to access disallowed key \'a\' in a | ||||||||||||||||||||||||||||
| 132 | # restricted hash'. man Const::Fast says: "You have to use exists | ||||||||||||||||||||||||||||
| 133 | # $a{baz} instead. This is a limitation of perl that can hopefully be | ||||||||||||||||||||||||||||
| 134 | # solved in the future." | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | TEST_EXCEPTION { $$empty_hash{a} = 1 } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 137 | 12 | 'Attempt to access disallowed key \'a\' in a restricted hash'; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 5 | ▶ | sub hash_set { | ||||||||||||||||||||||||||
|
Subroutine
hash_set: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 5 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 5 | my ($h, $k, $v) = @_; | |||||||||||||||||||||||||||
| 142 | 5 | my $h2 = +{%$h}; | |||||||||||||||||||||||||||
| 143 | 5 | $$h2{$k} = $v; | |||||||||||||||||||||||||||
| 144 | 5 | $h2 | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 12 | my $h = { a => 1, b => 2 }; | |||||||||||||||||||||||||||
| 148 | 1 | ▶ | TEST { hash_set $h, b => 3 } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 149 | 12 | +{ 'a' => 1, 'b' => 3 }; | |||||||||||||||||||||||||||
| 150 | 1 | ▶ | TEST {$h} | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 151 | 12 | +{ 'a' => 1, 'b' => 2 }; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | ▶ | sub hash_delete { | ||||||||||||||||||||||||||
|
Subroutine
hash_delete: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 1 | my ($h, $k) = @_; | |||||||||||||||||||||||||||
| 156 | 1 | my $h2 = +{%$h}; | |||||||||||||||||||||||||||
| 157 | 1 | delete $$h2{$k}; | |||||||||||||||||||||||||||
| 158 | 1 | $h2 | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 6 | ▶ | sub hash_update { | ||||||||||||||||||||||||||
|
Subroutine
hash_update: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 162 | 6 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 6 | my ($h, $k, $fn) = @_; | |||||||||||||||||||||||||||
| 164 | 6 | my $h2 = +{%$h}; | |||||||||||||||||||||||||||
| 165 | 6 | ▶ | if (my ($v) = &$fn(exists $$h{$k} ? $$h{$k} : ())) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 166 | 4 | $$h2{$k} = $v; | |||||||||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||||||||||
| 168 | 2 | delete $$h2{$k} | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 6 | $h2 | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 1 | ▶ | TEST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | 1 | ▶ | hash_update $h, 'a', sub { $_[0] + 10 } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | 1 | } | |||||||||||||||||||||||||||
| 176 | 12 | +{ a => 11, b => 2 }; | |||||||||||||||||||||||||||
| 177 | 1 | ▶ | TEST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | 1 | ▶ | hash_update $h, 'x', sub { [@_] } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | 1 | } | |||||||||||||||||||||||||||
| 180 | 12 | +{ a => 1, b => 2, x => [] }; | |||||||||||||||||||||||||||
| 181 | 1 | ▶ | TEST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 182 | 1 | ▶ | hash_update $h, 'a', sub { () } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 183 | 1 | } | |||||||||||||||||||||||||||
| 184 | 12 | +{ b => 2 }; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 2 | ▶ | sub hash_length { | ||||||||||||||||||||||||||
|
Subroutine
hash_length: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 187 | 2 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 2 | my ($h) = @_; | |||||||||||||||||||||||||||
| 189 | 2 | scalar keys %$h | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 1 | ▶ | TEST { hash_length +{} } 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | 1 | ▶ | TEST { hash_length +{ a => 4, b => 5 } } 2; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 39 | ▶ | sub hash_perhaps_ref { | ||||||||||||||||||||||||||
|
Subroutine
hash_perhaps_ref: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 196 | 39 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 39 | my ($h, $k) = @_; | |||||||||||||||||||||||||||
| 198 | 39 | ▶ | if (exists $$h{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 24 | $$h{$k} | |||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||
| 201 | () | ||||||||||||||||||||||||||||
| 202 | 15 | } | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # difference of the following to just $$h{$k} is that it won't die on | ||||||||||||||||||||||||||||
| 206 | # locked hashes | ||||||||||||||||||||||||||||
| 207 | 0 | ▶ | sub hash_maybe_ref { | ||||||||||||||||||||||||||
|
Subroutine
hash_maybe_ref: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | my ($h, $k) = @_; | |||||||||||||||||||||||||||
| 210 | 0 | ▶ | if (exists $$h{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | $$h{$k} | |||||||||||||||||||||||||||
| 212 | } else { | ||||||||||||||||||||||||||||
| 213 | undef | ||||||||||||||||||||||||||||
| 214 | 0 | } | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub hash_xref { | ||||||||||||||||||||||||||
|
Subroutine
hash_xref: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 0 | my ($h, $k) = @_; | |||||||||||||||||||||||||||
| 220 | 0 | ▶ | if (exists $$h{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | $$h{$k} | |||||||||||||||||||||||||||
| 222 | } else { | ||||||||||||||||||||||||||||
| 223 | 0 | die "unbound table key"; # no such key. unknown key. unbound | |||||||||||||||||||||||||||
| 224 | # hash key. ? | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | sub hash_ref_or { | ||||||||||||||||||||||||||
|
Subroutine
hash_ref_or: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | my ($h, $k, $other) = @_; | |||||||||||||||||||||||||||
| 231 | 0 | ▶ | if (exists $$h{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 0 | $$h{$k} | |||||||||||||||||||||||||||
| 233 | } else { | ||||||||||||||||||||||||||||
| 234 | 0 | $other | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # Curried hash lookup | ||||||||||||||||||||||||||||
| 239 | 2 | ▶ | sub hashkey { | ||||||||||||||||||||||||||
|
Subroutine
hashkey: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 240 | 2 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 2 | my ($key) = @_; | |||||||||||||||||||||||||||
| 242 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 243 | 3 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 3 | my ($h) = @_; | |||||||||||||||||||||||||||
| 245 | 3 | $h->{$key} | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 2 | } | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 1 | ▶ | sub mesh { | ||||||||||||||||||||||||||
|
Subroutine
mesh: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 250 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 1 | my ($keys, $values) = @_; | |||||||||||||||||||||||||||
| 252 | 1 | map { $keys->[$_] => $values->[$_] } 0 .. $#$keys | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 9 | ▶ | sub ziphash { | ||||||||||||||||||||||||||
|
Subroutine
ziphash: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 256 | 9 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 9 | my ($keys, $values) = @_; | |||||||||||||||||||||||||||
| 258 | 9 | +{ map { $keys->[$_] => $values->[$_] } 0 .. $#$keys } | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 12 | ▶ | sub hash_cache { | ||||||||||||||||||||||||||
|
Subroutine
hash_cache: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 12 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # only allowing for scalar context | ||||||||||||||||||||||||||||
| 266 | 12 | my ($h, $k, $generate) = @_; | |||||||||||||||||||||||||||
| 267 | 12 | ▶ | if (exists $$h{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 5 | $$h{$k} | |||||||||||||||||||||||||||
| 269 | } else { | ||||||||||||||||||||||||||||
| 270 | 7 | $$h{$k} = &$generate() | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # looking for definedness, not exists. Ok? Also, only handles strings | ||||||||||||||||||||||||||||
| 275 | # as values. | ||||||||||||||||||||||||||||
| 276 | 4 | ▶ | sub hash_diff { | ||||||||||||||||||||||||||
|
Subroutine
hash_diff: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 277 | 4 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 4 | my ($h1, $h2) = @_; | |||||||||||||||||||||||||||
| 279 | 4 | my $changes = {}; | |||||||||||||||||||||||||||
| 280 | 4 | for my $key (keys %$h2) { | |||||||||||||||||||||||||||
| 281 | 11 | my $old = $$h1{$key}; | |||||||||||||||||||||||||||
| 282 | 11 | my $new = $$h2{$key}; | |||||||||||||||||||||||||||
| 283 | 11 | ▶ | if (defined($old) and defined($new)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $old and defined $new
MC/DC100%2/2
defined $old and defined $new
defined $old
defined $new
Truth table100%3/3
A and B
A= defined $oldB= defined $new
| |||||||||||||||||||||||||||||
| 284 | 7 | ▶ | $$changes{$key} = ($old eq $new) ? "unchanged" : "changed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | } else { | ||||||||||||||||||||||||||||
| 286 | 4 | ▶ | $$changes{$key} = defined($old) ? "deleted" : "added"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 4 | for my $key (keys %$h1) { | |||||||||||||||||||||||||||
| 290 | 9 | ▶ | next if defined $$h2{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 2 | $$changes{$key} = "deleted"; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | $changes | ||||||||||||||||||||||||||||
| 294 | 4 | } | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 1 | ▶ | TEST { hash_diff { a => 1, b => 2 }, { a => 1, b => 3 } } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 297 | 12 | +{ 'a' => 'unchanged', 'b' => 'changed' }; | |||||||||||||||||||||||||||
| 298 | 1 | ▶ | TEST { hash_diff { a => 1, b => 2 }, { a => 1, b => 3, c => 5 } } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 299 | 12 | +{ 'c' => 'added', 'a' => 'unchanged', 'b' => 'changed' }; | |||||||||||||||||||||||||||
| 300 | 1 | ▶ | TEST { hash_diff { a => 1, b => 2 }, { b => 3, c => 5 } } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 301 | 12 | +{ 'c' => 'added', 'a' => 'deleted', 'b' => 'changed' }; | |||||||||||||||||||||||||||
| 302 | 1 | ▶ | TEST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 303 | 1 | hash_diff { a => 1, b => 2, x => 9 }, | |||||||||||||||||||||||||||
| 304 | { a => undef, b => 3, c => 5, x => 9 } | ||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | 12 | +{ 'c' => 'added', 'a' => 'deleted', 'b' => 'changed', 'x' => 'unchanged' }; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 3 | ▶ | sub subhash { | ||||||||||||||||||||||||||
|
Subroutine
subhash: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 309 | 3 | my $s = shift; | |||||||||||||||||||||||||||
| 310 | 3 | my %r; | |||||||||||||||||||||||||||
| 311 | 3 | for (@_) { | |||||||||||||||||||||||||||
| 312 | 6 | $r{$_} = $$s{$_} | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | 3 | \%r | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 1 | ▶ | TEST { subhash({ a => 10, b => 11, c => 12 }, "a", "c") } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 318 | 12 | +{ a => 10, c => 12 }; | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 12 | ▶ | use FP::HashSet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub hashes_keys { | ||||||||||||||||||||||||||
|
Subroutine
hashes_keys: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 323 | 0 | keys %{ array_to_hashset([map { keys %$_ } @_]) } | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 1 | ▶ | sub hash_map { | ||||||||||||||||||||||||||
|
Subroutine
hash_map: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 327 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 1 | my ($h, $fn) = @_; | |||||||||||||||||||||||||||
| 329 | 1 | +{ map { $fn->($_, $$h{$_}) } keys %$h } | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 1 | ▶ | sub hash_filter { | ||||||||||||||||||||||||||
|
Subroutine
hash_filter: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 333 | 1 | __ 'only keep k=>v entries for which $pred->($k, $v) is true'; | |||||||||||||||||||||||||||
| 334 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 1 | my ($hash, $pred) = @_; | |||||||||||||||||||||||||||
| 336 | +{ | ||||||||||||||||||||||||||||
| 337 | map { | ||||||||||||||||||||||||||||
| 338 | 1 | my $v = $hash->{$_}; | |||||||||||||||||||||||||||
| 339 | 4 | ▶ | $pred->($_, $v) ? ($_ => $v) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | } keys %$hash | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 1 | ▶ | sub hash_key_filter { | ||||||||||||||||||||||||||
|
Subroutine
hash_key_filter: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 345 | 1 | __ 'only keep k=>v entries for which $pred->($k) is true'; | |||||||||||||||||||||||||||
| 346 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | 1 | my ($hash, $pred) = @_; | |||||||||||||||||||||||||||
| 348 | 1 | ▶ | +{ map { $pred->($_) ? ($_ => $hash->{$_}) : () } keys %$hash } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 1 | ▶ | sub hash_value_filter { | ||||||||||||||||||||||||||
|
Subroutine
hash_value_filter: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 352 | 1 | __ 'only keep k=>v entries for which $pred->($v) is true'; | |||||||||||||||||||||||||||
| 353 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 1 | my ($hash, $pred) = @_; | |||||||||||||||||||||||||||
| 355 | +{ | ||||||||||||||||||||||||||||
| 356 | map { | ||||||||||||||||||||||||||||
| 357 | 1 | my $v = $hash->{$_}; | |||||||||||||||||||||||||||
| 358 | 4 | ▶ | $pred->($v) ? ($_ => $v) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | } keys %$hash | ||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | # set leafs in 2-level hash structure: | ||||||||||||||||||||||||||||
| 364 | 0 | ▶ | sub hash2_set { | ||||||||||||||||||||||||||
|
Subroutine
hash2_set: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | @_ == 4 or fp_croak_arity 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 0 | my ($h, $k0, $k1, $v) = @_; | |||||||||||||||||||||||||||
| 367 | 0 | ▶ | hash_update $h, $k0, sub { my ($h1) = @_; hash_set $h1, $k1, $v } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 368 | 0 | } | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 0 | ▶ | sub hash_to_maybefunction { | ||||||||||||||||||||||||||
|
Subroutine
hash_to_maybefunction: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 371 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 0 | my ($h) = @_; | |||||||||||||||||||||||||||
| 373 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 374 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 0 | my ($k) = @_; | |||||||||||||||||||||||||||
| 376 | 0 | $h->{$k} | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | 0 | } | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | ▶ | sub hash_to_perhapsfunction { | ||||||||||||||||||||||||||
|
Subroutine
hash_to_perhapsfunction: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 381 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | 0 | my ($h) = @_; | |||||||||||||||||||||||||||
| 383 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 384 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | 0 | my ($k) = @_; | |||||||||||||||||||||||||||
| 386 | 0 | ▶ | exists $h->{$k} ? $h->{$k} : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | 0 | } | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | sub hash_to_function { | ||||||||||||||||||||||||||
|
Subroutine
hash_to_function: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 391 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 0 | my ($h) = @_; | |||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 394 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 0 | my ($k) = @_; | |||||||||||||||||||||||||||
| 396 | 0 | ▶ | exists $h->{$k} ? $h->{$k} : die "unknown key" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | 0 | } | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 12 | 1 | |||||||||||||||||||||||||||