| 1 | package UR::Iterator; | ||||||||||
| 2 | |||||||||||
| 3 | 266 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 266 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 266 | our $VERSION = "0.47"; # UR $VERSION; | |||||||||
| 7 | |||||||||||
| 8 | 266 | 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 | 0 | ▶ | sub create { | ||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 14 | 0 | my $class = shift; | |||||||||
| 15 | 0 | Carp::croak("$class objects cannot be created via create(). Please see the documentation for more details"); | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | sub create_for_list { | ||||||||
|
Subroutine
create_for_list: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 19 | 0 | my $class = shift; | |||||||||
| 20 | 0 | my $items = \@_; | |||||||||
| 21 | |||||||||||
| 22 | 0 | foreach my $item ( @$items ) { | |||||||||
| 23 | 0 | ▶ | unless (defined $item) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | Carp::croak('undefined items are not allowed in an iterator list'); | |||||||||
| 25 | } | ||||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 0 | ▶ | my $code = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 29 | 0 | shift @$items; | |||||||||
| 30 | 0 | }; | |||||||||
| 31 | 0 | my $self = bless { _iteration_closure => $code }, __PACKAGE__; | |||||||||
| 32 | 0 | return $self; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 0 | ▶ | sub map($&) { | ||||||||
|
Subroutine
map: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 36 | 0 | my($self, $mapper) = @_; | |||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | my $wrapper = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 39 | 0 | local $_ = $self->next; | |||||||||
| 40 | 0 | ▶ | defined($_) ? $mapper->() : $_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | }; | |||||||||
| 42 | |||||||||||
| 43 | 0 | return bless { _iteration_closure => $wrapper }, __PACKAGE__; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | sub _iteration_closure { | ||||||||
|
Subroutine
_iteration_closure: not called CC=2 SCAR=17.9
| |||||||||||
| 47 | 0 | my $self = shift; | |||||||||
| 48 | 0 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | return $self->{_iteration_closure} = shift; | |||||||||
| 50 | } | ||||||||||
| 51 | 0 | $self->{_iteration_closure}; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | sub peek { | ||||||||
|
Subroutine
peek: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 56 | 0 | my $self = shift; | |||||||||
| 57 | 0 | ▶ | unless (exists $self->{peek_value}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | $self->{peek_value} = $self->{_iteration_closure}->(); | |||||||||
| 59 | } | ||||||||||
| 60 | 0 | $self->{peek_value}; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub next { | ||||||||
|
Subroutine
next: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 65 | 0 | my $self = shift; | |||||||||
| 66 | 0 | ▶ | if (exists $self->{peek_value}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | delete $self->{peek_value}; | |||||||||
| 68 | } else { | ||||||||||
| 69 | 0 | $self->{_iteration_closure}->(@_); | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub remaining { | ||||||||
|
Subroutine
remaining: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 74 | 0 | my $self = shift; | |||||||||
| 75 | 0 | my @remaining; | |||||||||
| 76 | 0 | ▶ | while (defined(my $o = $self->next )) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | push @remaining, $o; | |||||||||
| 78 | } | ||||||||||
| 79 | 0 | @remaining; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 266 | 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 | ||||||||||