| 1 | package UR::Iterator; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 7 | |||
| 8 | our @CARP_NOT = qw( UR::Object ); | ||
| 9 | |||
| 10 | # These are not UR Objects. They're regular blessed references that | ||
| 11 | # get garbage collected in the regular ways | ||
| 12 | |||
| 13 | sub create { | ||
| 14 | my $class = shift; | ||
| 15 | Carp::croak("$class objects cannot be created via create(). Please see the documentation for more details"); | ||
| 16 | } | ||
| 17 | |||
| 18 | sub create_for_list { | ||
| 19 | my $class = shift; | ||
| 20 | my $items = \@_; | ||
| 21 | |||
| 22 | foreach my $item ( @$items ) { | ||
| 23 | unless (defined $item) { | ||
| 24 | Carp::croak('undefined items are not allowed in an iterator list'); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | my $code = sub { | ||
| 29 | shift @$items; | ||
| 30 | }; | ||
| 31 | my $self = bless { _iteration_closure => $code }, __PACKAGE__; | ||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub map($&) { | ||
| 36 | my($self, $mapper) = @_; | ||
| 37 | |||
| 38 | my $wrapper = sub { | ||
| 39 | local $_ = $self->next; | ||
| 40 | defined($_) ? $mapper->() : $_; | ||
| 41 | }; | ||
| 42 | |||
| 43 | return bless { _iteration_closure => $wrapper }, __PACKAGE__; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _iteration_closure { | ||
| 47 | my $self = shift; | ||
| 48 | if (@_) { | ||
| 49 | return $self->{_iteration_closure} = shift; | ||
| 50 | } | ||
| 51 | $self->{_iteration_closure}; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub peek { | ||
| 56 | my $self = shift; | ||
| 57 | unless (exists $self->{peek_value}) { | ||
| 58 | $self->{peek_value} = $self->{_iteration_closure}->(); | ||
| 59 | } | ||
| 60 | $self->{peek_value}; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | sub next { | ||
| 65 | my $self = shift; | ||
| 66 | if (exists $self->{peek_value}) { | ||
| 67 | delete $self->{peek_value}; | ||
| 68 | } else { | ||
| 69 | $self->{_iteration_closure}->(@_); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub remaining { | ||
| 74 | my $self = shift; | ||
| 75 | my @remaining; | ||
| 76 | while (defined(my $o = $self->next )) { | ||
| 77 | push @remaining, $o; | ||
| 78 | } | ||
| 79 | @remaining; | ||
| 80 | } | ||
| 81 | |||
| 82 | 1; | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =head1 NAME | ||
| 87 | |||
| 88 | UR::Iterator - API for iterating through data | ||
| 89 | |||
| 90 | =head1 SYNOPSIS | ||
| 91 | |||
| 92 | my $iter = UR::Iterator->create_for_list(1, 2, 3, 4); | ||
| 93 | while (my $i = $iter->next) { | ||
| 94 | print $i\n"; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $mapped_iter = $iter->map(sub { $_ + 1 }); | ||
| 98 | while (my $i = $mapped_iter->next) { | ||
| 99 | print "$i\n"; | ||
| 100 | } | ||
| 101 | |||
| 102 | =head1 DESCRIPTION | ||
| 103 | |||
| 104 | UR::Iterator instances implement the iterator pattern. These objects can | ||
| 105 | be created with either a list of values, or by applying a mapping function | ||
| 106 | to another iterator. | ||
| 107 | |||
| 108 | UR::Iterator instances are normal Perl object references, not UR-based | ||
| 109 | objects. They do not live in the Context's object cache, and obey the | ||
| 110 | normal Perl rules about scoping. | ||
| 111 | |||
| 112 | =head1 METHODS | ||
| 113 | |||
| 114 | =over 4 | ||
| 115 | |||
| 116 | =item create_for_list | ||
| 117 | |||
| 118 | $iter = UR::Object::Iterator->create_for_list(@values); | ||
| 119 | |||
| 120 | Creates an iterator based on values contained in the given list. This | ||
| 121 | constructor will throw an exception if any of the supplied values is | ||
| 122 | C<undef>. | ||
| 123 | |||
| 124 | =item map | ||
| 125 | |||
| 126 | $new_iter = $iter->map(sub { $_ + 1 }); | ||
| 127 | |||
| 128 | Creates a new iterator based on an existing iterator. Values returned by this | ||
| 129 | new iterator are based on the values of the existing iterator after going | ||
| 130 | through a mapping function. This new iterator will be exhausted when the | ||
| 131 | original iterator is exhausted. | ||
| 132 | |||
| 133 | When the mapping function is called, C<$_> is set to the value obtained from | ||
| 134 | the original iterator. | ||
| 135 | |||
| 136 | =item next | ||
| 137 | |||
| 138 | $obj = $iter->next(); | ||
| 139 | |||
| 140 | Return the next object matching the iterator's rule. When there are no more | ||
| 141 | matching objects, it returns undef. | ||
| 142 | |||
| 143 | =item peek | ||
| 144 | |||
| 145 | $obj = $iter->peek(); | ||
| 146 | |||
| 147 | Return the next object matching the iterator's rule without removing it. The | ||
| 148 | next call to peek() or next() will return the same object. Returns undef if | ||
| 149 | there are no more matching objects. | ||
| 150 | |||
| 151 | This is useful to test whether a newly created iterator matched anything. | ||
| 152 | |||
| 153 | =item remaining | ||
| 154 | |||
| 155 | @objs = $iter->remaining(); | ||
| 156 | |||
| 157 | Return a list of all the objects remaining in the iterator. The list will be | ||
| 158 | empty if there is no more data. | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | =head1 SEE ALSO | ||
| 163 | |||
| 164 | L<UR::Object::Iterator> | ||
| 165 | |||
| 166 | =cut |