Subject: | more smart 3->times method |
I want to use
Show quoted text
> say $_ for 3->times;
follow is a patch.
=== Core.pm
==================================================================
--- Core.pm (revision 7192)
+++ Core.pm (local)
@@ -528,7 +528,13 @@
sub downto ($$) { [ CORE::reverse $_[1]..$_[0] ] }
# just weird, but cool
-sub times ($&) { for (0..$_[0]-1) { $_[1]->($_); }; $_[0]; }
+sub times ($;&) {
+ if ($_[1]) {
+ for (0..$_[0]-1) { $_[1]->($_); }; $_[0];
+ } else {
+ 0..$_[0]-1
+ }
+}
# doesn't minipulate scalars but works on scalars
=== t/added.t
==================================================================
--- t/added.t (revision 7192)
+++ t/added.t (local)
@@ -1,5 +1,5 @@
use Test;
-BEGIN { plan tests => 64 };
+BEGIN { plan tests => 66 };
use autobox;
use autobox::Core;
@@ -96,9 +96,12 @@
ok($a->[0] == 10 && $a->[@$a-1] == 1);
$a = 1;
-10->times(sub {$a++});
+ok(10->times(sub {$a++}) == 10);
ok($a == 11);
+$a = 1;
+$a++ for 10->times;
+ok($a == 11);
#####################################################################
# Hashes