6 use Test::More tests => 32;
11 use_ok('Moose::Role');
21 U(sub { my $h = shift; sub { $f->(U($h)->())->(@_) } })->();
37 sub head { (shift)->_list->[0] }
42 @{$self->_list}[1 .. $#{$self->_list}]
48 join ", " => @{$_[0]->_list};
51 package List::Immutable;
59 sub is_empty { not defined ($_[0]->head) }
66 my ($list, $acc) = @_;
67 return $acc if $list->is_empty;
68 $redo->($list->tail, $acc + 1);
74 my ($self, $function) = @_;
78 my ($list, $func, $acc) = @_;
79 return $list->new('::' => $acc)
84 [ @{$acc}, $func->($list->head) ]
87 }))->($self, $function, []);
96 with 'List', 'List::Immutable';
97 } '... successfully composed roles together';
105 with 'List::Immutable', 'List';
106 } '... successfully composed roles together';
111 my $coll = My::List1->new;
112 isa_ok($coll, 'My::List1');
114 ok($coll->does('List'), '... $coll does List');
115 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
117 ok($coll->is_empty, '... we have an empty collection');
118 is($coll->length, 0, '... we have a length of 1 for the collection');
122 my $coll = My::List2->new;
123 isa_ok($coll, 'My::List2');
125 ok($coll->does('List'), '... $coll does List');
126 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
128 ok($coll->is_empty, '... we have an empty collection');
129 is($coll->length, 0, '... we have a length of 1 for the collection');
133 my $coll = My::List1->new('::' => [ 1 .. 10 ]);
134 isa_ok($coll, 'My::List1');
136 ok($coll->does('List'), '... $coll does List');
137 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
139 ok(!$coll->is_empty, '... we do not have an empty collection');
140 is($coll->length, 10, '... we have a length of 10 for the collection');
142 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... got the right printed value');
144 my $coll2 = $coll->apply(sub { $_[0] * $_[0] });
145 isa_ok($coll2, 'My::List1');
147 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same');
148 is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed');
152 my $coll = My::List2->new('::' => [ 1 .. 10 ]);
153 isa_ok($coll, 'My::List2');
155 ok($coll->does('List'), '... $coll does List');
156 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
158 ok(!$coll->is_empty, '... we do not have an empty collection');
159 is($coll->length, 10, '... we have a length of 10 for the collection');
161 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... got the right printed value');
163 my $coll2 = $coll->apply(sub { $_[0] * $_[0] });
164 isa_ok($coll2, 'My::List2');
166 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same');
167 is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed');