6 use Test::More tests => 32;
11 use_ok('Moose::Role');
21 U(sub { my $h = shift; sub { $f->(U($h)->())->(@_) } })->();
35 sub head { (shift)->_list->[0] }
40 @{$self->_list}[1 .. $#{$self->_list}]
46 join ", " => @{$_[0]->_list};
49 package List::Immutable;
55 sub is_empty { not defined ($_[0]->head) }
62 my ($list, $acc) = @_;
63 return $acc if $list->is_empty;
64 $redo->($list->tail, $acc + 1);
70 my ($self, $function) = @_;
74 my ($list, $func, $acc) = @_;
75 return $list->new('::' => $acc)
80 [ @{$acc}, $func->($list->head) ]
83 }))->($self, $function, []);
90 with 'List', 'List::Immutable';
91 } '... successfully composed roles together';
97 with 'List::Immutable', 'List';
98 } '... successfully composed roles together';
103 my $coll = My::List1->new;
104 isa_ok($coll, 'My::List1');
106 ok($coll->does('List'), '... $coll does List');
107 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
109 ok($coll->is_empty, '... we have an empty collection');
110 is($coll->length, 0, '... we have a length of 1 for the collection');
114 my $coll = My::List2->new;
115 isa_ok($coll, 'My::List2');
117 ok($coll->does('List'), '... $coll does List');
118 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
120 ok($coll->is_empty, '... we have an empty collection');
121 is($coll->length, 0, '... we have a length of 1 for the collection');
125 my $coll = My::List1->new('::' => [ 1 .. 10 ]);
126 isa_ok($coll, 'My::List1');
128 ok($coll->does('List'), '... $coll does List');
129 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
131 ok(!$coll->is_empty, '... we do not have an empty collection');
132 is($coll->length, 10, '... we have a length of 10 for the collection');
134 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... got the right printed value');
136 my $coll2 = $coll->apply(sub { $_[0] * $_[0] });
137 isa_ok($coll2, 'My::List1');
139 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same');
140 is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed');
144 my $coll = My::List2->new('::' => [ 1 .. 10 ]);
145 isa_ok($coll, 'My::List2');
147 ok($coll->does('List'), '... $coll does List');
148 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
150 ok(!$coll->is_empty, '... we do not have an empty collection');
151 is($coll->length, 10, '... we have a length of 10 for the collection');
153 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... got the right printed value');
155 my $coll2 = $coll->apply(sub { $_[0] * $_[0] });
156 isa_ok($coll2, 'My::List2');
158 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same');
159 is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed');