Mojo/Collection.pm

filter: Statementstmt 100% 98 / 98 Branchbran 100% 24 / 24 Conditioncond 90.4% 19 / 21 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 34 / 34 Podpod 100% 22 / 22 totaltotal 97.6% 205 / 210 CC 21 SCAR 7.3
CC
21
Cov
99%
uniq
4.0
compact
3.0
each
3.0
CRAP 2.1
« Mojo/Server/Morbo/Backend.pm ↑ Summary Mojo/Reactor.pm »
1 package Mojo::Collection;
2 55 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 55 ▶use re qw(is_regexp);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 55 our @EXPORT_OK = ('c');
12  
13 1 ▶sub TO_JSON { [@{shift()}] }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
14  
15 60 ▶sub c { __PACKAGE__->new(@_) }
Subroutine c: called CC=1 SCAR=0.0
Pod documented
16  
17 3 ▶sub compact {
Subroutine compact: called CC=3 SCAR=11.0
Pod documented
18 3 my $self = shift;
19 3 ▶ return $self->new(grep { defined && (ref || length) } @$self);
Condition83%5/6
defined $_ && (ref $_ || length $_)
!ll&&!rl&&r
117
ref $_ or length $_
l!l&&r!l&&!r
071
MC/DC66%2/3
defined $_ && (ref $_ || length $_)
defined $_ ref $_ length $_
Truth table75%3/4
A && B or C
A= defined $_B= ref $_C= length $_
ABCresult
0XX0
1000
11X1
1011
20 }
21  
22 266 ▶sub each {
Subroutine each: called CC=3 SCAR=11.0
Pod documented
23 266 my ($self, $cb) = @_;
24 266 ▶ return @$self unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 153 113 266
25 113 my $i = 1;
26 113 $_->$cb($i++) for @$self;
27 113 return $self;
28 }
29  
30 84 ▶sub first {
Subroutine first: called CC=3 SCAR=11.0
Pod documented
31 84 my ($self, $cb) = (shift, shift);
32 84 ▶ return $self->[0] unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 73 11 84
33 11 ▶ return List::Util::first { $_ =~ $cb } @$self if is_regexp $cb;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &is_regexp($cb) 1 10 11
34 10 ▶ return List::Util::first { $_->$cb(@_) } @$self;
Subroutine __ANON__: called CC=1 SCAR=0.0
35 }
36  
37 5 ▶sub flatten { $_[0]->new(_flatten(@{$_[0]})) }
Subroutine flatten: called CC=1 SCAR=0.0
Pod documented
38  
39 35 ▶sub grep {
Subroutine grep: called CC=2 SCAR=6.9
Pod documented
40 35 my ($self, $cb) = (shift, shift);
41 35 ▶ return $self->new(grep { $_ =~ $cb } @$self) if is_regexp $cb;
Branch: 1 branch
BranchTrueFalseTotal
if &is_regexp($cb) 1 34 35
42 34 return $self->new(grep { $_->$cb(@_) } @$self);
43 }
44  
45 9 ▶sub head {
Subroutine head: called CC=3 SCAR=11.0
Pod documented
46 9 my ($self, $size) = @_;
47 9 ▶ return $self->new(@$self) if $size > @$self;
Branch: 1 branch
BranchTrueFalseTotal
if $size > @$self 1 8 9
48 8 ▶ return $self->new(@$self[0 .. ($size - 1)]) if $size >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size >= 0 4 4 8
49 4 return $self->new(@$self[0 .. ($#$self + $size)]);
50 }
51  
52 52 ▶sub join {
Subroutine join: called CC=2 SCAR=6.9
Pod documented
53 52 ▶ Mojo::ByteStream->new(join $_[1] // '', map {"$_"} @{$_[0]});
Condition100%2/2
$_[1] // ""
l!l
493
MC/DC100%1/1
$_[1] // ""
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
54 }
55  
56 24 ▶sub last { shift->[-1] }
Subroutine last: called CC=1 SCAR=0.0
Pod documented
57  
58 156 ▶sub map {
Subroutine map: called CC=1 SCAR=0.0
Pod documented
59 156 my ($self, $cb) = (shift, shift);
60 156 return $self->new(map { $_->$cb(@_) } @$self);
61 }
62  
63 1064 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
64 1064 my $class = shift;
65 1064 ▶ return bless [@_], ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
2408240
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
66 }
67  
68 3 ▶sub reduce {
Subroutine reduce: called CC=1 SCAR=0.0
Pod documented
69 3 my $self = shift;
70 3 @_ = (@_, @$self);
71 3 goto &List::Util::reduce;
72 }
73  
74 8 ▶sub reverse { $_[0]->new(reverse @{$_[0]}) }
Subroutine reverse: called CC=1 SCAR=0.0
Pod documented
75  
76 2 ▶sub shuffle { $_[0]->new(List::Util::shuffle @{$_[0]}) }
Subroutine shuffle: called CC=1 SCAR=0.0
Pod documented
77  
78 60 ▶sub size { scalar @{$_[0]} }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
79  
80 6 ▶sub sort {
Subroutine sort: called CC=2 SCAR=6.9
Pod documented
81 6 my ($self, $cb) = @_;
82  
83 6 ▶ return $self->new(sort @$self) unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 2 4 6
84  
85 4 my $caller = caller;
86 55 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 my @sorted = sort {
88 4 local (*{"${caller}::a"}, *{"${caller}::b"}) = (\$a, \$b);
89 12 $a->$cb($b);
90 } @$self;
91 4 return $self->new(@sorted);
92 }
93  
94 9 ▶sub tail {
Subroutine tail: called CC=3 SCAR=11.0
Pod documented
95 9 my ($self, $size) = @_;
96 9 ▶ return $self->new(@$self) if $size > @$self;
Branch: 1 branch
BranchTrueFalseTotal
if $size > @$self 1 8 9
97 8 ▶ return $self->new(@$self[($#$self - ($size - 1)) .. $#$self]) if $size >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size >= 0 4 4 8
98 4 return $self->new(@$self[(0 - $size) .. $#$self]);
99 }
100  
101 1 ▶sub tap { shift->Mojo::Base::tap(@_) }
Subroutine tap: called CC=1 SCAR=0.0
Pod documented
102  
103 85 ▶sub to_array { [@{shift()}] }
Subroutine to_array: called CC=1 SCAR=0.0
Pod documented
104  
105 7 ▶sub uniq {
Subroutine uniq: called CC=4 SCAR=13.9
Pod documented
106 7 my ($self, $cb) = (shift, shift);
107 7 my %seen;
108 7 ▶ return $self->new(grep { !$seen{$_->$cb(@_) // ''}++ } @$self) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 3 4 7
Condition100%2/2
$_->$cb(@_) // ""
l!l
111
MC/DC100%1/1
$_->$cb(@_) // ""
$_->$cb(@_)
Truth table100%2/2
A
A= $_->$cb(@_)
Aresult
11
00
109 4 ▶ return $self->new(grep { !$seen{$_ // ''}++ } @$self);
Condition100%2/2
$_ // ""
l!l
261
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
110 }
111  
112 1 ▶sub with_roles { shift->Mojo::Base::with_roles(@_) }
Subroutine with_roles: called CC=1 SCAR=0.0
Pod documented
113  
114 16 ▶sub _flatten {
Subroutine _flatten: called CC=2 SCAR=6.9
115 16 ▶ map { _ref($_) ? _flatten(@$_) : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
_ref($_) ? : 11 30 41
116 }
117  
118 41 ▶sub _ref { ref $_[0] eq 'ARRAY' || blessed $_[0] && $_[0]->isa(__PACKAGE__) }
Subroutine _ref: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) 3 38 41
Condition100%6/6
ref $_[0] eq 'ARRAY' || builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__)
l!l&&r!l&&!r
3830
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
2648
MC/DC100%3/3
ref $_[0] eq 'ARRAY' || builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__)
ref $_[0] eq 'ARRAY' builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table100%4/4
A || B and C
A= ref $_[0] eq 'ARRAY'B= builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)
ABCresult
1XX1
0111
00X0
0100
119  
120 55 1;
121  
122 =encoding utf8
123  
124 =head1 NAME
125  
126 Mojo::Collection - Collection
127  
128 =head1 SYNOPSIS
129  
130 use Mojo::Collection;
131  
132 # Manipulate collection
133 my $collection = Mojo::Collection->new(qw(just works));
134 unshift @$collection, 'it';
135 say $collection->join("\n");
136  
137 # Chain methods
138 $collection->map(sub { ucfirst })->shuffle->each(sub ($word, $num) {
139 say "$num: $word";
140 });
141  
142 # Use the alternative constructor
143 use Mojo::Collection qw(c);
144 c(qw(a b c))->join('/')->url_escape->say;
145  
146 =head1 DESCRIPTION
147  
148 L<Mojo::Collection> is an array-based container for collections.
149  
150 # Access array directly to manipulate collection
151 my $collection = Mojo::Collection->new(1 .. 25);
152 $collection->[23] += 100;
153 say for @$collection;
154  
155 =head1 FUNCTIONS
156  
157 L<Mojo::Collection> implements the following functions, which can be imported individually.
158  
159 =head2 c
160  
161 my $collection = c(1, 2, 3);
162  
163 Construct a new array-based L<Mojo::Collection> object.
164  
165 =head1 METHODS
166  
167 L<Mojo::Collection> implements the following methods.
168  
169 =head2 TO_JSON
170  
171 my $array = $collection->TO_JSON;
172  
173 Alias for L</"to_array">.
174  
175 =head2 compact
176  
177 my $new = $collection->compact;
178  
179 Create a new collection with all elements that are defined and not an empty string.
180  
181 # "0, 1, 2, 3"
182 c(0, 1, undef, 2, '', 3)->compact->join(', ');
183  
184 =head2 each
185  
186 my @elements = $collection->each;
187 $collection = $collection->each(sub {...});
188  
189 Evaluate callback for each element in collection, or return all elements as a list if none has been provided. The
190 element will be the first argument passed to the callback, and is also available as C<$_>.
191  
192 # Make a numbered list
193 $collection->each(sub ($e, $num) {
194 say "$num: $e";
195 });
196  
197 =head2 first
198  
199 my $first = $collection->first;
200 my $first = $collection->first(qr/foo/);
201 my $first = $collection->first(sub {...});
202 my $first = $collection->first('some_method');
203 my $first = $collection->first('some_method', @args);
204  
205 Evaluate regular expression/callback for, or call method on, each element in collection and return the first one that
206 matched the regular expression, or for which the callback/method returned true. The element will be the first argument
207 passed to the callback, and is also available as C<$_>.
208  
209 # Longer version
210 my $first = $collection->first(sub { $_->some_method(@args) });
211  
212 # Find first value that contains the word "mojo"
213 my $interesting = $collection->first(qr/mojo/i);
214  
215 # Find first value that is greater than 5
216 my $greater = $collection->first(sub { $_ > 5 });
217  
218 =head2 flatten
219  
220 my $new = $collection->flatten;
221  
222 Flatten nested collections/arrays recursively and create a new collection with all elements.
223  
224 # "1, 2, 3, 4, 5, 6, 7"
225 c(1, [2, [3, 4], 5, [6]], 7)->flatten->join(', ');
226  
227 =head2 grep
228  
229 my $new = $collection->grep(qr/foo/);
230 my $new = $collection->grep(sub {...});
231 my $new = $collection->grep('some_method');
232 my $new = $collection->grep('some_method', @args);
233  
234 Evaluate regular expression/callback for, or call method on, each element in collection and create a new collection
235 with all elements that matched the regular expression, or for which the callback/method returned true. The element will
236 be the first argument passed to the callback, and is also available as C<$_>.
237  
238 # Longer version
239 my $new = $collection->grep(sub { $_->some_method(@args) });
240  
241 # Find all values that contain the word "mojo"
242 my $interesting = $collection->grep(qr/mojo/i);
243  
244 # Find all values that are greater than 5
245 my $greater = $collection->grep(sub { $_ > 5 });
246  
247 =head2 head
248  
249 my $new = $collection->head(4);
250 my $new = $collection->head(-2);
251  
252 Create a new collection with up to the specified number of elements from the beginning of the collection. A negative
253 number will count from the end.
254  
255 # "A B C"
256 c('A', 'B', 'C', 'D', 'E')->head(3)->join(' ');
257  
258 # "A B"
259 c('A', 'B', 'C', 'D', 'E')->head(-3)->join(' ');
260  
261 =head2 join
262  
263 my $stream = $collection->join;
264 my $stream = $collection->join("\n");
265  
266 Turn collection into L<Mojo::ByteStream>.
267  
268 # Join all values with commas
269 $collection->join(', ')->say;
270  
271 =head2 last
272  
273 my $last = $collection->last;
274  
275 Return the last element in collection.
276  
277 =head2 map
278  
279 my $new = $collection->map(sub {...});
280 my $new = $collection->map('some_method');
281 my $new = $collection->map('some_method', @args);
282  
283 Evaluate callback for, or call method on, each element in collection and create a new collection from the results. The
284 element will be the first argument passed to the callback, and is also available as C<$_>.
285  
286 # Longer version
287 my $new = $collection->map(sub { $_->some_method(@args) });
288  
289 # Append the word "mojo" to all values
290 my $mojoified = $collection->map(sub { $_ . 'mojo' });
291  
292 =head2 new
293  
294 my $collection = Mojo::Collection->new(1, 2, 3);
295  
296 Construct a new array-based L<Mojo::Collection> object.
297  
298 =head2 reduce
299  
300 my $result = $collection->reduce(sub {...});
301 my $result = $collection->reduce(sub {...}, $initial);
302  
303 Reduce elements in collection with a callback and return its final result, setting C<$a> and C<$b> each time the
304 callback is executed. The first time C<$a> will be set to an optional initial value or the first element in the
305 collection. And from then on C<$a> will be set to the return value of the callback, while C<$b> will always be set to
306 the next element in the collection.
307  
308 # Calculate the sum of all values
309 my $sum = $collection->reduce(sub { $a + $b });
310  
311 # Count how often each value occurs in collection
312 my $hash = $collection->reduce(sub { $a->{$b}++; $a }, {});
313  
314 =head2 reverse
315  
316 my $new = $collection->reverse;
317  
318 Create a new collection with all elements in reverse order.
319  
320 =head2 shuffle
321  
322 my $new = $collection->shuffle;
323  
324 Create a new collection with all elements in random order.
325  
326 =head2 size
327  
328 my $size = $collection->size;
329  
330 Number of elements in collection.
331  
332 =head2 sort
333  
334 my $new = $collection->sort;
335 my $new = $collection->sort(sub {...});
336  
337 Sort elements based on return value of a callback and create a new collection from the results, setting C<$a> and C<$b>
338 to the elements being compared, each time the callback is executed.
339  
340 # Sort values case-insensitive
341 my $case_insensitive = $collection->sort(sub { uc($a) cmp uc($b) });
342  
343 =head2 tail
344  
345 my $new = $collection->tail(4);
346 my $new = $collection->tail(-2);
347  
348 Create a new collection with up to the specified number of elements from the end of the collection. A negative number
349 will count from the beginning.
350  
351 # "C D E"
352 c('A', 'B', 'C', 'D', 'E')->tail(3)->join(' ');
353  
354 # "D E"
355 c('A', 'B', 'C', 'D', 'E')->tail(-3)->join(' ');
356  
357 =head2 tap
358  
359 $collection = $collection->tap(sub {...});
360  
361 Alias for L<Mojo::Base/"tap">.
362  
363 =head2 to_array
364  
365 my $array = $collection->to_array;
366  
367 Turn collection into array reference.
368  
369 =head2 uniq
370  
371 my $new = $collection->uniq;
372 my $new = $collection->uniq(sub {...});
373 my $new = $collection->uniq('some_method');
374 my $new = $collection->uniq('some_method', @args);
375  
376 Create a new collection without duplicate elements, using the string representation of either the elements or the
377 return value of the callback/method to decide uniqueness. Note that C<undef> and empty string are treated the same.
378  
379 # Longer version
380 my $new = $collection->uniq(sub { $_->some_method(@args) });
381  
382 # "foo bar baz"
383 c('foo', 'bar', 'bar', 'baz')->uniq->join(' ');
384  
385 # "[[1, 2], [2, 1]]"
386 c([1, 2], [2, 1], [3, 2])->uniq(sub{ $_->[1] })->to_array;
387  
388 =head2 with_roles
389  
390 my $new_class = Mojo::Collection->with_roles('Mojo::Collection::Role::One');
391 my $new_class = Mojo::Collection->with_roles('+One', '+Two');
392 $collection = $collection->with_roles('+One', '+Two');
393  
394 Alias for L<Mojo::Base/"with_roles">.
395  
396 =head1 SEE ALSO
397  
398 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
399  
400 =cut
« Mojo/Server/Morbo/Backend.pm ↑ Summary Mojo/Reactor.pm »