6 use Test::More tests => 30;
16 U(sub { my $h = shift; sub { $f->(U($h)->())->(@_) } })->();
30 sub head { (shift)->_list->[0] }
35 @{$self->_list}[1 .. $#{$self->_list}]
41 join ", " => @{$_[0]->_list};
44 package List::Immutable;
50 sub is_empty { not defined ($_[0]->head) }
57 my ($list, $acc) = @_;
58 return $acc if $list->is_empty;
59 $redo->($list->tail, $acc + 1);
65 my ($self, $function) = @_;
69 my ($list, $func, $acc) = @_;
70 return (ref $list)->new('::' => $acc)
75 [ @{$acc}, $func->($list->head) ]
78 }))->($self, $function, []);
85 with 'List', 'List::Immutable';
86 } '... successfully composed roles together';
92 with 'List::Immutable', 'List';
93 } '... successfully composed roles together';
98 my $coll = My::List1->new;
99 isa_ok($coll, 'My::List1');
101 ok($coll->does('List'), '... $coll does List');
102 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
104 ok($coll->is_empty, '... we have an empty collection');
105 is($coll->length, 0, '... we have a length of 1 for the collection');
109 my $coll = My::List2->new;
110 isa_ok($coll, 'My::List2');
112 ok($coll->does('List'), '... $coll does List');
113 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
115 ok($coll->is_empty, '... we have an empty collection');
116 is($coll->length, 0, '... we have a length of 1 for the collection');
120 my $coll = My::List1->new('::' => [ 1 .. 10 ]);
121 isa_ok($coll, 'My::List1');
123 ok($coll->does('List'), '... $coll does List');
124 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
126 ok(!$coll->is_empty, '... we do not have an empty collection');
127 is($coll->length, 10, '... we have a length of 10 for the collection');
129 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... got the right printed value');
131 my $coll2 = $coll->apply(sub { $_[0] * $_[0] });
132 isa_ok($coll2, 'My::List1');
134 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same');
135 is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed');
139 my $coll = My::List2->new('::' => [ 1 .. 10 ]);
140 isa_ok($coll, 'My::List2');
142 ok($coll->does('List'), '... $coll does List');
143 ok($coll->does('List::Immutable'), '... $coll does List::Immutable');
145 ok(!$coll->is_empty, '... we do not have an empty collection');
146 is($coll->length, 10, '... we have a length of 10 for the collection');
148 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... got the right printed value');
150 my $coll2 = $coll->apply(sub { $_[0] * $_[0] });
151 isa_ok($coll2, 'My::List2');
153 is($coll->print, '1, 2, 3, 4, 5, 6, 7, 8, 9, 10', '... original is still the same');
154 is($coll2->print, '1, 4, 9, 16, 25, 36, 49, 64, 81, 100', '... new collection is changed');