1Mojo::Collection(3)   User Contributed Perl Documentation  Mojo::Collection(3)
2
3
4

NAME

6       Mojo::Collection - Collection
7

SYNOPSIS

9         use Mojo::Collection;
10
11         # Manipulate collection
12         my $collection = Mojo::Collection->new(qw(just works));
13         unshift @$collection, 'it';
14         say $collection->join("\n");
15
16         # Chain methods
17         $collection->map(sub { ucfirst })->shuffle->each(sub {
18           my ($word, $num) = @_;
19           say "$num: $word";
20         });
21
22         # Use the alternative constructor
23         use Mojo::Collection 'c';
24         c(qw(a b c))->join('/')->url_escape->say;
25

DESCRIPTION

27       Mojo::Collection is an array-based container for collections.
28
29         # Access array directly to manipulate collection
30         my $collection = Mojo::Collection->new(1 .. 25);
31         $collection->[23] += 100;
32         say for @$collection;
33

FUNCTIONS

35       Mojo::Collection implements the following functions, which can be
36       imported individually.
37
38   c
39         my $collection = c(1, 2, 3);
40
41       Construct a new array-based Mojo::Collection object.
42

METHODS

44       Mojo::Collection implements the following methods.
45
46   TO_JSON
47         my $array = $collection->TO_JSON;
48
49       Alias for "to_array".
50
51   compact
52         my $new = $collection->compact;
53
54       Create a new collection with all elements that are defined and not an
55       empty string.
56
57         # "0, 1, 2, 3"
58         c(0, 1, undef, 2, '', 3)->compact->join(', ');
59
60   each
61         my @elements = $collection->each;
62         $collection  = $collection->each(sub {...});
63
64       Evaluate callback for each element in collection, or return all
65       elements as a list if none has been provided. The element will be the
66       first argument passed to the callback, and is also available as $_.
67
68         # Make a numbered list
69         $collection->each(sub {
70           my ($e, $num) = @_;
71           say "$num: $e";
72         });
73
74   first
75         my $first = $collection->first;
76         my $first = $collection->first(qr/foo/);
77         my $first = $collection->first(sub {...});
78         my $first = $collection->first('some_method');
79         my $first = $collection->first('some_method', @args);
80
81       Evaluate regular expression/callback for, or call method on, each
82       element in collection and return the first one that matched the regular
83       expression, or for which the callback/method returned true. The element
84       will be the first argument passed to the callback, and is also
85       available as $_.
86
87         # Longer version
88         my $first = $collection->first(sub { $_->some_method(@args) });
89
90         # Find first value that contains the word "mojo"
91         my $interesting = $collection->first(qr/mojo/i);
92
93         # Find first value that is greater than 5
94         my $greater = $collection->first(sub { $_ > 5 });
95
96   flatten
97         my $new = $collection->flatten;
98
99       Flatten nested collections/arrays recursively and create a new
100       collection with all elements.
101
102         # "1, 2, 3, 4, 5, 6, 7"
103         c(1, [2, [3, 4], 5, [6]], 7)->flatten->join(', ');
104
105   grep
106         my $new = $collection->grep(qr/foo/);
107         my $new = $collection->grep(sub {...});
108         my $new = $collection->grep('some_method');
109         my $new = $collection->grep('some_method', @args);
110
111       Evaluate regular expression/callback for, or call method on, each
112       element in collection and create a new collection with all elements
113       that matched the regular expression, or for which the callback/method
114       returned true. The element will be the first argument passed to the
115       callback, and is also available as $_.
116
117         # Longer version
118         my $new = $collection->grep(sub { $_->some_method(@args) });
119
120         # Find all values that contain the word "mojo"
121         my $interesting = $collection->grep(qr/mojo/i);
122
123         # Find all values that are greater than 5
124         my $greater = $collection->grep(sub { $_ > 5 });
125
126   join
127         my $stream = $collection->join;
128         my $stream = $collection->join("\n");
129
130       Turn collection into Mojo::ByteStream.
131
132         # Join all values with commas
133         $collection->join(', ')->say;
134
135   last
136         my $last = $collection->last;
137
138       Return the last element in collection.
139
140   map
141         my $new = $collection->map(sub {...});
142         my $new = $collection->map('some_method');
143         my $new = $collection->map('some_method', @args);
144
145       Evaluate callback for, or call method on, each element in collection
146       and create a new collection from the results. The element will be the
147       first argument passed to the callback, and is also available as $_.
148
149         # Longer version
150         my $new = $collection->map(sub { $_->some_method(@args) });
151
152         # Append the word "mojo" to all values
153         my $mojoified = $collection->map(sub { $_ . 'mojo' });
154
155   new
156         my $collection = Mojo::Collection->new(1, 2, 3);
157
158       Construct a new array-based Mojo::Collection object.
159
160   reduce
161         my $result = $collection->reduce(sub {...});
162         my $result = $collection->reduce(sub {...}, $initial);
163
164       Reduce elements in collection with a callback and return its final
165       result, setting $a and $b each time the callback is executed. The first
166       time $a will be set to an optional initial value or the first element
167       in the collection.  And from then on $a will be set to the return value
168       of the callback, while $b will always be set to the next element in the
169       collection.
170
171         # Calculate the sum of all values
172         my $sum = $collection->reduce(sub { $a + $b });
173
174         # Count how often each value occurs in collection
175         my $hash = $collection->reduce(sub { $a->{$b}++; $a }, {});
176
177   reverse
178         my $new = $collection->reverse;
179
180       Create a new collection with all elements in reverse order.
181
182   slice
183         my $new = $collection->slice(4 .. 7);
184
185       Create a new collection with all selected elements.
186
187         # "B C E"
188         c('A', 'B', 'C', 'D', 'E')->slice(1, 2, 4)->join(' ');
189
190   shuffle
191         my $new = $collection->shuffle;
192
193       Create a new collection with all elements in random order.
194
195   size
196         my $size = $collection->size;
197
198       Number of elements in collection.
199
200   sort
201         my $new = $collection->sort;
202         my $new = $collection->sort(sub {...});
203
204       Sort elements based on return value of a callback and create a new
205       collection from the results, setting $a and $b to the elements being
206       compared, each time the callback is executed.
207
208         # Sort values case-insensitive
209         my $case_insensitive = $collection->sort(sub { uc($a) cmp uc($b) });
210
211   tap
212         $collection = $collection->tap(sub {...});
213
214       Alias for "tap" in Mojo::Base.
215
216   to_array
217         my $array = $collection->to_array;
218
219       Turn collection into array reference.
220
221   uniq
222         my $new = $collection->uniq;
223         my $new = $collection->uniq(sub {...});
224         my $new = $collection->uniq('some_method');
225         my $new = $collection->uniq('some_method', @args);
226
227       Create a new collection without duplicate elements, using the string
228       representation of either the elements or the return value of the
229       callback/method to decide uniqueness. Note that "undef" and empty
230       string are treated the same.
231
232         # Longer version
233         my $new = $collection->uniq(sub { $_->some_method(@args) });
234
235         # "foo bar baz"
236         c('foo', 'bar', 'bar', 'baz')->uniq->join(' ');
237
238         # "[[1, 2], [2, 1]]"
239         c([1, 2], [2, 1], [3, 2])->uniq(sub{ $_->[1] })->to_array;
240
241   with_roles
242         my $new_class = Mojo::Collection->with_roles('Mojo::Collection::Role::One');
243         my $new_class = Mojo::Collection->with_roles('+One', '+Two');
244         $collection   = $collection->with_roles('+One', '+Two');
245
246       Alias for "with_roles" in Mojo::Base.
247

SEE ALSO

249       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
250
251
252
253perl v5.28.1                      2018-11-22               Mojo::Collection(3)
Impressum