| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Mojo::Collection::Role::Iterable; | ||
| 4 | |||
| 5 | use Role::Tiny; | ||
| 6 | |||
| 7 | use Hash::Util::FieldHash qw(fieldhash); | ||
| 8 | |||
| 9 | # ABSTRACT: Iterator role for Mojo::Collection | ||
| 10 | |||
| 11 | fieldhash my %idx; | ||
| 12 | fieldhash my %_iter_pos; | ||
| 13 | |||
| 14 | use overload | ||
| 15 | '++' => sub { shift->increment }, | ||
| 16 | '--' => sub { shift->decrement }, | ||
| 17 | "fallback" => 1; | ||
| 18 | |||
| 19 | |||
| 20 | sub idx { | ||
| 21 | my ($self, $val) = @_; | ||
| 22 | $idx{$self} //= 0; | ||
| 23 | $idx{$self} = $val if @_ > 1; | ||
| 24 | return $idx{$self}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _iter_pos { | ||
| 28 | my ($self, $val) = @_; | ||
| 29 | $_iter_pos{$self} //= 0; | ||
| 30 | $_iter_pos{$self} = $val if @_ > 1; | ||
| 31 | return $_iter_pos{$self}; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub prev { $_[0]->idx > 0 ? $_[0]->[$_[0]->idx - 1] : undef } | ||
| 35 | sub curr { @{$_[0]} ? $_[0]->[$_[0]->idx] : undef }; *current = \&curr; *item = \&curr; | ||
| 36 | sub next { $_[0]->idx < (-1 + scalar $_[0]->@*) ? $_[0]->[$_[0]->idx + 1] : undef } | ||
| 37 | |||
| 38 | sub increment { $_[0]->idx($_[0]->idx + 1) if $_[0]->idx < -1 + scalar $_[0]->@*; return $_[0] } | ||
| 39 | sub decrement { $_[0]->idx($_[0]->idx - 1) if $_[0]->idx > 0; return $_[0] } | ||
| 40 | |||
| 41 | |||
| 42 | sub reset { | ||
| 43 | $_[0]->idx(0); | ||
| 44 | $_[0]->_iter_pos(0); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub iterate { | ||
| 48 | my $self = shift; | ||
| 49 | return () unless @$self; # empty collection fast-exit | ||
| 50 | my $pos = $self->_iter_pos; | ||
| 51 | return () if $pos >= scalar @$self; | ||
| 52 | my $ret = $self->[$pos]; | ||
| 53 | if ($pos < scalar $self->@*) { | ||
| 54 | $self->idx($self->_iter_pos); | ||
| 55 | } | ||
| 56 | $self->_iter_pos($pos + 1); | ||
| 57 | return $ret; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub each { | ||
| 61 | my ($self, $cb) = @_; | ||
| 62 | return $self->@* unless $cb; | ||
| 63 | |||
| 64 | my $i = 1; | ||
| 65 | $self->reset; | ||
| 66 | for ($self->@*) { | ||
| 67 | $_->$cb($i++); | ||
| 68 | $self->increment; | ||
| 69 | } | ||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | |||
| 78 | =encoding utf8 | ||
| 79 | |||
| 80 | =head1 NAME | ||
| 81 | |||
| 82 | Mojo::Collection::Role::Iterable - Iterator capabilities as a composable role for Mojo::Collection | ||
| 83 | |||
| 84 | =head1 SYNOPSIS | ||
| 85 | |||
| 86 | use Mojo::Collection; | ||
| 87 | |||
| 88 | my $c = Mojo::Collection->with_roles('+Iterable')->new(qw(foo bar baz)); | ||
| 89 | |||
| 90 | # Sequential iteration | ||
| 91 | while (defined(my $item = $c->iterate)) { | ||
| 92 | say $item; | ||
| 93 | } | ||
| 94 | |||
| 95 | # Cursor movement | ||
| 96 | $c->reset; | ||
| 97 | say $c->curr; # foo | ||
| 98 | $c->increment; | ||
| 99 | say $c->curr; # bar | ||
| 100 | say $c->next; # baz | ||
| 101 | say $c->prev; # foo | ||
| 102 | |||
| 103 | # Rewind and go again | ||
| 104 | $c->reset; | ||
| 105 | |||
| 106 | =head1 DESCRIPTION | ||
| 107 | |||
| 108 | L<Mojo::Collection::Role::Iterable> is a L<Mojo::Role> for | ||
| 109 | iterator-style cursor access in L<Mojo::Collection>. | ||
| 110 | |||
| 111 | =head1 METHODS | ||
| 112 | |||
| 113 | =head2 curr / current / item | ||
| 114 | |||
| 115 | my $thing = $c->curr; | ||
| 116 | |||
| 117 | Returns the element at the current cursor position. | ||
| 118 | |||
| 119 | =head2 prev | ||
| 120 | |||
| 121 | my $thing = $c->prev; | ||
| 122 | |||
| 123 | Returns the element before the cursor, or C<undef> at the beginning. | ||
| 124 | |||
| 125 | =head2 next | ||
| 126 | |||
| 127 | my $thing = $c->next; | ||
| 128 | |||
| 129 | Returns the element after the cursor, or C<undef> at the end. | ||
| 130 | |||
| 131 | =head2 increment | ||
| 132 | |||
| 133 | $c->increment; # chainable | ||
| 134 | |||
| 135 | Moves the cursor forward by one (no-op at the last element). | ||
| 136 | |||
| 137 | =head2 decrement | ||
| 138 | |||
| 139 | $c->decrement; # chainable | ||
| 140 | |||
| 141 | Moves the cursor backward by one (no-op at the first element). | ||
| 142 | |||
| 143 | =head2 reset | ||
| 144 | |||
| 145 | $c->reset; # chainable | ||
| 146 | |||
| 147 | Rewinds both the cursor and the C<iterate> position to 0. | ||
| 148 | |||
| 149 | =head2 iterate | ||
| 150 | |||
| 151 | while (defined(my $item = $c->iterate)) { ... } | ||
| 152 | |||
| 153 | Returns the item at the current iteration position and advances it, | ||
| 154 | keeping the cursor in sync. Returns C<undef> when exhausted. | ||
| 155 | Call C<reset> to iterate again. | ||
| 156 | |||
| 157 | =head3 CAVEAT | ||
| 158 | |||
| 159 | The safe iteration idiom is: | ||
| 160 | |||
| 161 | while (my ($item) = $c->iterate) { ... } | ||
| 162 | |||
| 163 | Note the parentheses around C<$item> — this is a list assignment, not a scalar | ||
| 164 | one. The loop terminates only when C<iterate> returns an empty list, which | ||
| 165 | happens exclusively when the collection is exhausted. This means the loop | ||
| 166 | handles false values (C<0>, C<"">, C<"0">) and even C<undef> as legitimate | ||
| 167 | collection items without terminating early. | ||
| 168 | |||
| 169 | The simpler forms are tempting but have failure modes: | ||
| 170 | |||
| 171 | while (my $item = $c->iterate) { ... } # terminates early on 0, "", undef | ||
| 172 | while (defined(my $item = $c->iterate)) { ... } # terminates early on undef | ||
| 173 | |||
| 174 | Use these only if you can guarantee your collection contains no false or | ||
| 175 | C<undef> values respectively. When in doubt, use the list form. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | =head1 OVERLOADING | ||
| 180 | |||
| 181 | This role installs two operator overloads: | ||
| 182 | |||
| 183 | =over 4 | ||
| 184 | |||
| 185 | =item C<++> | ||
| 186 | |||
| 187 | Calls C<increment>, moving the cursor forward by one. The collection | ||
| 188 | contents are not affected; only the cursor position changes. | ||
| 189 | |||
| 190 | =item C<--> | ||
| 191 | |||
| 192 | Calls C<decrement>, moving the cursor backward by one. Same caveat applies. | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | =head1 SEE ALSO | ||
| 197 | |||
| 198 | L<Mojo::Collection>, L<Mojo::Base>, L<Hash::Util::FieldHash> | ||
| 199 | |||
| 200 | =head1 AUTHOR | ||
| 201 | |||
| 202 | Simone Cesano <scesano@cpan.org> | ||
| 203 | |||
| 204 | =head1 COPYRIGHT AND LICENSE | ||
| 205 | |||
| 206 | Copyright (C) Simone Cesano. | ||
| 207 | |||
| 208 | This library is free software; you can redistribute it and/or modify | ||
| 209 | it under the same terms as Perl itself. | ||
| 210 | |||
| 211 | =cut |