From: Stevan Little Date: Sun, 4 Jun 2006 15:56:53 +0000 (+0000) Subject: foo X-Git-Tag: 0_02~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=252ab1a26175a028754adbe4589cd87bc5240ce6;p=gitmo%2FMoose-Autobox.git foo --- diff --git a/lib/Moose/Autobox.pm b/lib/Moose/Autobox.pm index 7b9838f..5af7ff5 100644 --- a/lib/Moose/Autobox.pm +++ b/lib/Moose/Autobox.pm @@ -4,7 +4,7 @@ package Moose::Autobox; use strict; use warnings; -use Moose (); +use Moose qw(confess); use Scalar::Util (); our $VERSION = '0.01'; @@ -27,6 +27,7 @@ package CODE; use Moose; with 'Moose::Autobox::Code'; |; + confess 'Could not create autobox packages because - ' . $@ if $@; } 1; diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index c58198e..defa83b 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -1,5 +1,6 @@ package Moose::Autobox::Array; use Moose::Role 'with'; +use autobox; our $VERSION = '0.01'; @@ -43,23 +44,22 @@ sub sort { sub reduce { my ($array, $func) = @_; - my @a = @$array; - my $acc = CORE::shift @a; - $acc = $func->($acc, $_) foreach @a; + my $a = $array->values; + my $acc = $a->shift; + $a->map(sub { $acc = $func->($acc, $_) }); return $acc; } sub zip { my ($array, $other) = @_; - [ - CORE::map { - [ $array->[$_], $other->[$_] ] - } 0 .. $#{( - CORE::scalar @{$array} < CORE::scalar @{$other} - ? $other : $array - )} - ]; -} + ($array->length < $other->length + ? $other + : $array) + ->keys + ->map(sub { + [ $array->[$_], $other->[$_] ] + }); +} ## @@ -111,5 +111,4 @@ sub shift { CORE::shift @$array; } - 1; diff --git a/lib/Moose/Autobox/Code.pm b/lib/Moose/Autobox/Code.pm index f58b9a7..72a529f 100644 --- a/lib/Moose/Autobox/Code.pm +++ b/lib/Moose/Autobox/Code.pm @@ -1,5 +1,6 @@ package Moose::Autobox::Code; use Moose::Role 'with'; +use autobox; our $VERSION = '0.01'; diff --git a/lib/Moose/Autobox/Defined.pm b/lib/Moose/Autobox/Defined.pm index a71010a..210b22b 100644 --- a/lib/Moose/Autobox/Defined.pm +++ b/lib/Moose/Autobox/Defined.pm @@ -7,4 +7,10 @@ with 'Moose::Autobox::Item'; sub defined { 1 } +sub do { + my ($self, $block) = @_; + local $_ = $self; + $block->($self); +} + 1; \ No newline at end of file diff --git a/lib/Moose/Autobox/Item.pm b/lib/Moose/Autobox/Item.pm index f7316b2..61cd996 100644 --- a/lib/Moose/Autobox/Item.pm +++ b/lib/Moose/Autobox/Item.pm @@ -1,6 +1,8 @@ package Moose::Autobox::Item; -use Moose::Role 'with'; +use Moose::Role 'requires'; our $VERSION = '0.01'; +requires 'defined'; + 1; \ No newline at end of file diff --git a/t/000_load.t b/t/000_load.t index 7650751..32c9d47 100644 --- a/t/000_load.t +++ b/t/000_load.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 1; BEGIN { use_ok('Moose::Autobox'); diff --git a/t/001_basic.t b/t/001_basic.t index 79274b9..8bd55e5 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 44; BEGIN { use_ok('Moose::Autobox'); @@ -44,12 +44,12 @@ is_deeply( ok((sub { 1 })->disjoin(sub { 0 })->(), '... disjoins properly'); ok(!(sub { 1 })->conjoin(sub { 0 })->(), '... conjoins properly'); -#my $compose = (sub { 1, @_ })->compose(sub { 2, @_ }); +my $compose = (sub { @_, 1 })->compose(sub { @_, 2 }); -#is_deeply( -#[ $compose->() ], -#[ 1, 2 ], -#'... got the right return value for compose'); +is_deeply( +[ $compose->() ], +[ 1, 2 ], +'... got the right return value for compose'); # ARRAY diff --git a/t/002_example.t b/t/002_example.t index 83d7337..f2d0a54 100644 --- a/t/002_example.t +++ b/t/002_example.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 19; BEGIN { use_ok('Moose::Autobox'); diff --git a/t/003_p6_example.t b/t/003_p6_example.t index 4be1352..60cd248 100644 --- a/t/003_p6_example.t +++ b/t/003_p6_example.t @@ -3,47 +3,48 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 7; +use Test::Exception; BEGIN { use_ok('Moose::Autobox'); } -use autobox; - -SCALAR->meta->add_method('bytes' => sub { - $_[0]; -}); - -SCALAR->meta->add_method('byte' => SCALAR->meta->get_method('bytes')); - -SCALAR->meta->add_method('kilobytes' => sub { - $_[0] * 1024; -}); - -SCALAR->meta->add_method('kilobyte' => SCALAR->meta->get_method('kilobytes')); - -SCALAR->meta->add_method('megabytes' => sub { - $_[0] * 1024->kilobytes; -}); - -SCALAR->meta->add_method('metabyte' => SCALAR->meta->get_method('megabytes')); - -SCALAR->meta->add_method('gigabytes' => sub { - $_[0] * 1024->megabytes; -}); - -SCALAR->meta->add_method('gigabyte' => SCALAR->meta->get_method('gigabytes')); +{ + package Bytes; + use Moose::Role; + use autobox; + + sub bytes { $_[0] } + sub kilobytes { $_[0] * 1024 } + sub megabytes { $_[0] * 1024->kilobytes } + sub gigabytes { $_[0] * 1024->megabytes } + sub terabytes { $_[0] * 1024->gigabytes } + + { + no warnings; # << squelch the stupid "used only once, maybe typo" warnings + *byte = \&bytes; + *kilobyte = \&kilobytes; + *megabyte = \&megabytes; + *gigabyte = \&gigabytes; + *terabyte = \&terabytes; + } +} -SCALAR->meta->add_method('terabytes' => sub { - $_[0] * 1024->gigabytes; -}); +{ + package SCALAR; + use Moose; + with 'Bytes'; +} -SCALAR->meta->add_method('terabyte' => SCALAR->meta->get_method('terabytes')); +{ + use autobox; -is(5->bytes, 5, '... got 5 bytes'); -is(5->kilobytes, 5120, '... got 5 kilobytes'); -is(2->megabytes, 2097152, '... got 2 megabytes'); -is(1->gigabyte, 1073741824, '... got 1 gigabyte'); -is(2->terabytes, 2199023255552, '... got 2 terabyte'); + is(5->bytes, 5, '... got 5 bytes'); + is(5->kilobytes, 5120, '... got 5 kilobytes'); + is(2->megabytes, 2097152, '... got 2 megabytes'); + is(1->gigabyte, 1073741824, '... got 1 gigabyte'); + is(2->terabytes, 2199023255552, '... got 2 terabyte'); +} +dies_ok { 5->bytes } '... no longer got 5 bytes'; diff --git a/t/004_list_compressions.t b/t/004_list_compressions.t new file mode 100644 index 0000000..d97775f --- /dev/null +++ b/t/004_list_compressions.t @@ -0,0 +1,22 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; + +BEGIN { + use_ok('Moose::Autobox'); +} + +use autobox; + +is_deeply( +[ 1 .. 5 ]->map(sub { $_ * $_ }), +[ 1, 4, 9, 16, 25 ], +'... got the expected return values'); + +is_deeply( +[ 1 .. 5 ]->map(sub { $_ * $_ })->do(sub { $_->zip($_) }), +[ [1, 1], [4, 4], [9, 9], [16, 16], [25, 25] ], +'... got the expected return values'); \ No newline at end of file