From: Ricardo Signes Date: Mon, 12 Oct 2009 15:07:26 +0000 (-0400) Subject: each, each_key, each_value for indexed X-Git-Tag: 0_10~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose-Autobox.git;a=commitdiff_plain;h=2dcdd7d70c44b88f199af083cc1ee68c072b5a16 each, each_key, each_value for indexed --- diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index 74a45d2..21ce08a 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -117,6 +117,27 @@ sub kv { $array->keys->map(sub { [ $_, $array->[$_] ] }); } +sub each { + my ($array, $sub) = @_; + for my $i (0 .. $#$array) { + $sub->($i, $array->[ $i ]); + } +} + +sub each_key { + my ($array, $sub) = @_; + for my $i (0 .. $#$array) { + $sub->($i); + } +} + +sub each_value { + my ($array, $sub) = @_; + $sub->($_) for @$array; +} + +# end indexed + sub flatten { @{$_[0]} } diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm index 84b8c31..67f94b1 100644 --- a/lib/Moose/Autobox/Hash.pm +++ b/lib/Moose/Autobox/Hash.pm @@ -62,7 +62,27 @@ sub kv { sub slice { my ($hash, $keys) = @_; return [ @{$hash}{@$keys} ]; -}; +} + +sub each { + my ($hash, $sub) = @_; + for my $key (CORE::keys %$hash) { + $sub->($key, $hash->{$key}); + } +} + +sub each_key { + my ($hash, $sub) = @_; + $sub->($_) for CORE::keys %$hash; +} + +sub each_value { + my ($hash, $sub) = @_; + $sub->($_) for CORE::values %$hash; +} + + +# End Indexed sub print { CORE::print %{$_[0]} } sub say { CORE::print %{$_[0]}, "\n" } diff --git a/lib/Moose/Autobox/Indexed.pm b/lib/Moose/Autobox/Indexed.pm index 551a28c..e2f5362 100644 --- a/lib/Moose/Autobox/Indexed.pm +++ b/lib/Moose/Autobox/Indexed.pm @@ -10,6 +10,7 @@ requires 'keys'; requires 'values'; requires 'kv'; requires 'slice'; +requires qw(each each_key each_value); 1; @@ -54,6 +55,14 @@ and L implement this role. =item B +=item B + +=item B + +=item B + +=item B + =back =head1 BUGS