| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Hash::MostUtils; | ||
| 4 | use base qw(Exporter); | ||
| 5 | |||
| 6 | use Carp qw(confess); | ||
| 7 | use Hash::MostUtils::leach qw(n_each leach); | ||
| 8 | |||
| 9 | our $VERSION = 1.05; | ||
| 10 | |||
| 11 | our @EXPORT_OK = qw( | ||
| 12 | lvalues | ||
| 13 | lkeys | ||
| 14 | leach | ||
| 15 | hash_slice_of | ||
| 16 | hash_slice_by | ||
| 17 | hashmap | ||
| 18 | hashgrep | ||
| 19 | hashapply | ||
| 20 | n_each | ||
| 21 | n_map | ||
| 22 | n_grep | ||
| 23 | n_apply | ||
| 24 | reindex | ||
| 25 | rekey | ||
| 26 | revalue | ||
| 27 | ); | ||
| 28 | |||
| 29 | # decrementing $| flips it between 0 and 1 | ||
| 30 | sub lkeys { local $|; return grep { $|-- == 0 } @_ } | ||
| 31 | sub lvalues { local $|; return grep { $|-- == 1 } @_ } | ||
| 32 | |||
| 33 | # I would put leach() here, but it was imported above | ||
| 34 | |||
| 35 | *hashmap = sub(&@) { unshift @_, 2; goto &n_map }; | ||
| 36 | *hashgrep = sub(&@) { unshift @_, 2; goto &n_grep }; | ||
| 37 | *hashapply = sub (&@) { unshift @_, 2; goto &n_apply }; | ||
| 38 | |||
| 39 | # I would put n_each() here, but it was imported above | ||
| 40 | |||
| 41 | sub n_map ($&@) { | ||
| 42 | # Usually I don't mutate @_. Here I deliberately modify @_ for the upcoming non-obvious goto-&NAME. | ||
| 43 | my $n = shift; | ||
| 44 | my $collector = sub { return $_[0]->() }; | ||
| 45 | unshift @_, $collector; | ||
| 46 | |||
| 47 | # Using a "safe goto" allows n_map() to remove itself from the callstack, which allows _n_collect() | ||
| 48 | # to see the correct caller. | ||
| 49 | # | ||
| 50 | # 'perldoc -f goto' for why this is a safe goto. | ||
| 51 | goto &{_n_collect($n)}; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub n_grep ($&@) { | ||
| 55 | my $n = shift; | ||
| 56 | |||
| 57 | # the comments in n_map() apply here as well. | ||
| 58 | |||
| 59 | my $collector = sub { | ||
| 60 | my ($code, $vals, $aliases) = @_; | ||
| 61 | return $code->() ? @$vals : (); | ||
| 62 | }; | ||
| 63 | unshift @_, $collector; | ||
| 64 | |||
| 65 | goto &{_n_collect($n)}; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub n_apply { | ||
| 69 | my $n = shift; | ||
| 70 | my $collector = sub { | ||
| 71 | my ($code, $vals, $aliases) = @_; | ||
| 72 | $code->(); | ||
| 73 | return map { $$_ } @$aliases; | ||
| 74 | }; | ||
| 75 | unshift @_, $collector; | ||
| 76 | |||
| 77 | goto &{_n_collect($n)}; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _n_collect($) { | ||
| 81 | my ($n) = @_; | ||
| 82 | return sub(&@) { | ||
| 83 | my $collector = shift; | ||
| 84 | my $code = shift; | ||
| 85 | if (@_ % $n != 0) { | ||
| 86 | confess("your input is insane: can't evenly slice " . @_ . " elements into $n-sized chunks\n"); | ||
| 87 | } | ||
| 88 | |||
| 89 | # these'll reserve some namespace back in the callpackage | ||
| 90 | my @n = ('a' .. 'z'); | ||
| 91 | |||
| 92 | # stash old values back in callpackage *and* in main. If called from main::, this comes down to: | ||
| 93 | # local ${'main::a'}, ${'main::b'}, ${'main::c'} | ||
| 94 | # when $n is 3. | ||
| 95 | my $caller = caller; | ||
| 96 | no strict 'refs'; | ||
| 97 | foreach ((@n[ 0 .. $n-1 ])) { | ||
| 98 | local ${"$caller\::$_"}; | ||
| 99 | local ${"::$_"}; | ||
| 100 | } | ||
| 101 | |||
| 102 | my @out; | ||
| 103 | while (my @chunk = splice @_, 0, $n) { # build up each set... | ||
| 104 | my @aliases; | ||
| 105 | foreach (0 .. $#chunk) { | ||
| 106 | # ...assign values from @_ back to localized variables in $caller *and* in 'main::'. | ||
| 107 | # Aliasing in main:: allows you to refer to variables $c and onwards as $::c. | ||
| 108 | # Aliasing in $caller allows you to refer to variables $c and onwards as $whatever::package::c. | ||
| 109 | ${"::$n[$_]"} = ${"$caller\::$n[$_]"} = $chunk[$_]; | ||
| 110 | |||
| 111 | # Keep a reference to $::a (etc.) and pass them in to the $collector; this allows $code to mutate | ||
| 112 | # $::a (etc) and signal the changed values back to $collector. | ||
| 113 | push @aliases, \${"::$n[$_]"}; | ||
| 114 | } | ||
| 115 | push @out, $collector->($code, \@chunk, \@aliases); # ...and apply $code. | ||
| 116 | } | ||
| 117 | |||
| 118 | return @out; | ||
| 119 | }; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub hash_slice_of { | ||
| 123 | my ($ref, @keys) = @_; | ||
| 124 | return map { ($_ => $ref->{$_}) } @keys; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub hash_slice_by { | ||
| 128 | my ($obj, @methods) = @_; | ||
| 129 | return map { ($_ => scalar($obj->$_)) } @methods; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub rekey (&@) { | ||
| 133 | my %map = shift()->(); | ||
| 134 | return n_map 2, sub { $map{$a} || $a => $b }, @_; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub reindex (&@) { | ||
| 138 | my %map = shift()->(); | ||
| 139 | @_[values %map] = delete @_[keys %map]; | ||
| 140 | return @_; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub revalue (&@) { | ||
| 144 | my %map = shift()->(); | ||
| 145 | return n_map 2, sub { $a => $map{$b} || $b }, @_; | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; | ||
| 149 | |||
| 150 | __END__ |