Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Data / Stream / Bulk / Array.pm
1 #!/usr/bin/perl
2
3 package Data::Stream::Bulk::Array;
4 use Moose;
5
6 use namespace::clean -except => 'meta';
7
8 with qw(Data::Stream::Bulk) => { excludes => [qw/loaded filter list_cat/] };
9
10 has array => (
11         isa => "ArrayRef",
12         reader  => "_array",
13         writer  => "_set_array",
14         clearer => "_clear_array",
15         predicate => "_has_array",
16         required => 1,
17 );
18
19 sub is_done {
20         my $self = shift;
21         !$self->_has_array;
22 }
23
24 sub next {
25         my $self = shift;
26
27         if ( my $array = $self->_array ) {
28                 $self->_clear_array;
29                 return $array;
30         } else {
31                 return;
32         }
33 }
34
35 # squish several arrays into one
36 sub list_cat {
37         my ( $self, @rest ) = @_;
38
39         return $self unless @rest;
40
41         my @arrays = ( $self );
42
43         # fetch all adjacent arrays
44         push @arrays, shift @rest while @rest and $rest[0]->isa(__PACKAGE__);
45
46         if ( @arrays > 1 ) {
47                 my @cat;
48                 push @cat, @$_ for map { $_->_array } @arrays;
49                 return __PACKAGE__->new(
50                         array => \@cat,
51                 )->cat( @rest );
52         } else {
53                 my $head = shift @rest;
54                 return ( $self, $head->list_cat(@rest) );
55         }
56 }
57
58 sub filter {
59         my ( $self, $filter ) = @_;
60         local $_ = $self->next;
61         $self->_set_array( $filter->($_) );
62         return $self;
63 }
64
65 sub loaded { 1 }
66
67 __PACKAGE__->meta->make_immutable;
68
69 __PACKAGE__
70
71 __END__
72
73 =pod
74
75 =head1 NAME
76
77 Data::Stream::Bulk::Array - L<Data::Stream::Bulk> wrapper for simple arrays.
78
79 =head1 SYNOPSIS
80
81         return Data::Stream::Bulk::Array->new(
82                 array => \@results,
83         );
84
85 =head1 DESCRIPTION
86
87 This implementation of the L<Data::Stream::Bulk> api wraps an array.
88
89 The use case is to keep the consumer of the data set implementation agnostic so
90 that it can deal with larger data sets if they are encountered, but still
91 retain most of the simplicity when the current data set easily fits in memory.
92
93 =head1 ATTRIBUTES
94
95 =over 4
96
97 =item array
98
99 The array reference to wrap.
100
101 =back
102
103 =head1 METHODS
104
105 =over 4
106
107 =item next
108
109 Returns the array reference on the first invocation, and nothing thereafter.
110
111 =item is_done
112
113 Returns true if C<next> has been called.
114
115 =item list_cat
116
117 Squishes adjacent arrays into a new array.
118
119 =item filter $filter
120
121 Immediately applies C<$filter> to the internal array and returns C<$self>.
122
123 =item loaded
124
125 Returns true
126
127 =back
128
129 =cut