File: Array.pod

package info (click to toggle)
libsub-handlesvia-perl 0.052000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,740 kB
  • sloc: perl: 9,645; makefile: 2
file content (686 lines) | stat: -rw-r--r-- 17,953 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
=head1 NAME

Sub::HandlesVia::HandlerLibrary::Array - library of array-related methods

=head1 SYNOPSIS

  package My::Class {
    use Moo;
    use Sub::HandlesVia;
    use Types::Standard 'ArrayRef';
    has attr => (
      is => 'rwp',
      isa => ArrayRef,
      handles_via => 'Array',
      handles => {
        'my_accessor' => 'accessor',
        'my_all' => 'all',
        'my_all_true' => 'all_true',
        'my_any' => 'any',
        'my_apply' => 'apply',
        'my_clear' => 'clear',
        'my_count' => 'count',
        'my_delete' => 'delete',
        'my_elements' => 'elements',
        'my_first' => 'first',
        'my_first_index' => 'first_index',
        'my_flatten' => 'flatten',
        'my_flatten_deep' => 'flatten_deep',
        'my_for_each' => 'for_each',
        'my_for_each_pair' => 'for_each_pair',
        'my_get' => 'get',
        'my_grep' => 'grep',
        'my_head' => 'head',
        'my_indexed' => 'indexed',
        'my_insert' => 'insert',
        'my_is_empty' => 'is_empty',
        'my_join' => 'join',
        'my_map' => 'map',
        'my_max' => 'max',
        'my_maxstr' => 'maxstr',
        'my_min' => 'min',
        'my_minstr' => 'minstr',
        'my_natatime' => 'natatime',
        'my_not_all_true' => 'not_all_true',
        'my_pairfirst' => 'pairfirst',
        'my_pairgrep' => 'pairgrep',
        'my_pairkeys' => 'pairkeys',
        'my_pairmap' => 'pairmap',
        'my_pairs' => 'pairs',
        'my_pairvalues' => 'pairvalues',
        'my_pick_random' => 'pick_random',
        'my_pop' => 'pop',
        'my_print' => 'print',
        'my_product' => 'product',
        'my_push' => 'push',
        'my_reduce' => 'reduce',
        'my_reductions' => 'reductions',
        'my_reset' => 'reset',
        'my_reverse' => 'reverse',
        'my_sample' => 'sample',
        'my_set' => 'set',
        'my_shallow_clone' => 'shallow_clone',
        'my_shift' => 'shift',
        'my_shuffle' => 'shuffle',
        'my_shuffle_in_place' => 'shuffle_in_place',
        'my_sort' => 'sort',
        'my_sort_in_place' => 'sort_in_place',
        'my_splice' => 'splice',
        'my_sum' => 'sum',
        'my_tail' => 'tail',
        'my_uniq' => 'uniq',
        'my_uniq_in_place' => 'uniq_in_place',
        'my_uniqnum' => 'uniqnum',
        'my_uniqnum_in_place' => 'uniqnum_in_place',
        'my_uniqstr' => 'uniqstr',
        'my_uniqstr_in_place' => 'uniqstr_in_place',
        'my_unshift' => 'unshift',
      },
    );
  }

=head1 DESCRIPTION

This is a library of methods for L<Sub::HandlesVia>.

=head1 DELEGATABLE METHODS

=head2 C<< accessor( $index, $value? ) >>

Arguments: B<< Int >>, B<< Optional[Any] >>.

Acts like C<get> if given just an index, or C<set> if given an index and value.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  $object->my_accessor( 1, 'quux' );
  say Dumper( $object->attr ); ## ==> [ 'foo', 'quux', 'baz' ]
  say $object->my_accessor( 2 ); ## ==> 'baz'

=head2 C<< all() >>

All elements in the array, in list context.

  my $object = My::Class->new( attr => [ 'foo', 'bar' ] );
  my @list = $object->my_all;
  say Dumper( \@list ); ## ==> [ 'foo', 'bar' ]

=head2 C<< all_true( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::all() >>.

=head2 C<< any( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::any() >>.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  my $truth  = $object->my_any( sub { /a/ } );
  say $truth; ## ==> true

=head2 C<< apply( $coderef ) >>

Arguments: B<< CodeRef >>.

Executes the coderef (which should modify C<< $_ >>) against each element of the array; returns the resulting array in list context.

=head2 C<< clear() >>

Empties the array.

  my $object = My::Class->new( attr => [ 'foo' ] );
  $object->my_clear;
  say Dumper( $object->attr ); ## ==> []

=head2 C<< count() >>

The number of elements in the referenced array.

  my $object = My::Class->new( attr => [ 'foo', 'bar' ] );
  say $object->my_count; ## ==> 2

=head2 C<< delete( $index ) >>

Arguments: B<< Int >>.

Removes the indexed element from the array and returns it. Elements after it will be "moved up".

=head2 C<< elements() >>

All elements in the array, in list context. (Essentially the same as C<all>.)

  my $object = My::Class->new( attr => [ 'foo', 'bar' ] );
  my @list = $object->my_elements;
  say Dumper( \@list ); ## ==> [ 'foo', 'bar' ]

=head2 C<< first( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::first() >>.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  my $found  = $object->my_first( sub { /a/ } );
  say $found; ## ==> 'bar'

=head2 C<< first_index( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::MoreUtils::first_index() >>.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  my $found  = $object->my_first_index( sub { /z$/ } );
  say $found; ## ==> 2

=head2 C<< flatten() >>

All elements in the array, in list context. (Essentially the same as C<all>.)

  my $object = My::Class->new( attr => [ 'foo', 'bar' ] );
  my @list = $object->my_flatten;
  say Dumper( \@list ); ## ==> [ 'foo', 'bar' ]

=head2 C<< flatten_deep( $depth? ) >>

Arguments: B<< Optional[Int] >>.

Flattens the arrayref into a list, including any nested arrayrefs. (Has the potential to loop infinitely.)

  my $object = My::Class->new( attr => [ 'foo', [ 'bar', [ 'baz' ] ] ] );
  say Dumper( [ $object->my_flatten_deep ] ); ## ==> [ 'foo', 'bar', 'baz' ]

  my $object2 = My::Class->new( attr => [ 'foo', [ 'bar', [ 'baz' ] ] ] );
  say Dumper( [ $object->my_flatten_deep(1) ] ); ## ==> [ 'foo', 'bar', [ 'baz' ] ]

=head2 C<< for_each( $coderef ) >>

Arguments: B<< CodeRef >>.

Chainable method which executes the coderef on each element of the array. The coderef will be passed two values: the element and its index. The element will also be available as C<< $_ >>.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  $object->my_for_each( sub { say "Item $_[1] is $_[0]." } );

=head2 C<< for_each_pair( $coderef ) >>

Arguments: B<< CodeRef >>.

Chainable method which executes the coderef on each pair of elements in the array. The coderef will be passed the two elements.

=head2 C<< get( $index ) >>

Arguments: B<< Int >>.

Returns a single element from the array by index.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  say $object->my_get(  0 ); ## ==> 'foo'
  say $object->my_get(  1 ); ## ==> 'bar'
  say $object->my_get( -1 ); ## ==> 'baz'

=head2 C<< grep( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<grep> from L<perlfunc>.

=head2 C<< head( $count ) >>

Arguments: B<< Int >>.

Returns the first C<< $count >> elements of the array in list context.

=head2 C<< indexed() >>

Like C<indexed> from L<builtin>.

=head2 C<< insert( $index, $value ) >>

Arguments: B<< Int >>, B<< Any >>.

Inserts a value into the array with the given index. Elements after it will be "moved down".

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  $object->my_insert( 1, 'quux' );
  say Dumper( $object->attr ); ## ==> [ 'foo', 'quux', 'bar', 'baz' ]

=head2 C<< is_empty() >>

Boolean indicating if the referenced array is empty.

  my $object = My::Class->new( attr => [ 'foo', 'bar' ] );
  say $object->my_is_empty; ## ==> false
  $object->_set_attr( [] );
  say $object->my_is_empty; ## ==> true

=head2 C<< join( $with? ) >>

Arguments: B<< Optional[Str] >>.

Returns a string joining all the elements in the array; if C<< $with >> is omitted, defaults to a comma.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  say $object->my_join;        ## ==> 'foo,bar,baz'
  say $object->my_join( '|' ); ## ==> 'foo|bar|baz'

=head2 C<< map( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<map> from L<perlfunc>.

=head2 C<< max() >>

Like C<< List::Util::max() >>.

=head2 C<< maxstr() >>

Like C<< List::Util::maxstr() >>.

=head2 C<< min() >>

Like C<< List::Util::min() >>.

=head2 C<< minstr() >>

Like C<< List::Util::minstr() >>.

=head2 C<< natatime( $n, $callback? ) >>

Arguments: B<< Int >>, B<< Optional[CodeRef] >>.

Given just a number, returns an iterator which reads that many elements from the array at a time. If also given a callback, calls the callback repeatedly with those values.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  my $iter   = $object->my_natatime( 2 );
  say Dumper( [ $iter->() ] ); ## ==> [ 'foo', 'bar' ]
  say Dumper( [ $iter->() ] ); ## ==> [ 'baz' ]

=head2 C<< not_all_true( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::notall() >>.

=head2 C<< pairfirst( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::pairfirst() >>.

=head2 C<< pairgrep( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::pairgrep() >>.

=head2 C<< pairkeys() >>

Like C<< List::Util::pairkeys() >>.

=head2 C<< pairmap( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::pairmap() >>.

=head2 C<< pairs() >>

Like C<< List::Util::pairs() >>.

=head2 C<< pairvalues() >>

Like C<< List::Util::pairvalues() >>.

=head2 C<< pick_random( $count ) >>

Arguments: B<< Optional[Int] >>.

If no C<< $count >> is given, returns one element of the array at random. If C<< $count >> is given, creates a new array with that many random elements from the original array (or fewer if the original array is not long enough) and returns that as an arrayref or list depending on context

=head2 C<< pop() >>

Removes the last element from the array and returns it.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  say $object->my_pop; ## ==> 'baz'
  say $object->my_pop; ## ==> 'bar'
  say Dumper( $object->attr ); ## ==> [ 'foo' ]

=head2 C<< print( $fh?, $with? ) >>

Arguments: B<< Optional[FileHandle] >>, B<< Optional[Str] >>.

Prints a string joining all the elements in the array; if C<< $fh >> is omitted, defaults to STDOUT; if C<< $with >> is omitted, defaults to a comma.

=head2 C<< product() >>

Like C<< List::Util::product() >>.

=head2 C<< push( @values ) >>

Adds elements to the end of the array.

  my $object = My::Class->new( attr => [ 'foo' ] );
  $object->my_push( 'bar', 'baz' );
  say Dumper( $object->attr ); ## ==> [ 'foo', 'bar', 'baz' ]

=head2 C<< reduce( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::reduce() >>.

=head2 C<< reductions( $coderef ) >>

Arguments: B<< CodeRef >>.

Like C<< List::Util::reductions() >>.

=head2 C<< reset() >>

Resets the attribute to its default value, or an empty arrayref if it has no default.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  $object->my_reset;
  say Dumper( $object->attr ); ## ==> []

=head2 C<< reverse() >>

Returns the reversed array in list context.

=head2 C<< sample( $count ) >>

Arguments: B<< Int >>.

Like C<< List::Util::sample() >>.

=head2 C<< set( $index, $value ) >>

Arguments: B<< Int >>, B<< Any >>.

Sets the element with the given index to the supplied value.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  $object->my_set( 1, 'quux' );
  say Dumper( $object->attr ); ## ==> [ 'foo', 'quux', 'baz' ]

=head2 C<< shallow_clone() >>

Creates a new arrayref with the same elements as the original.

=head2 C<< shift() >>

Removes an element from the start of the array and returns it.

  my $object = My::Class->new( attr => [ 'foo', 'bar', 'baz' ] );
  say $object->my_shift; ## ==> 'foo'
  say $object->my_shift; ## ==> 'bar'
  say Dumper( $object->attr ); ## ==> [ 'baz' ]

=head2 C<< shuffle() >>

Returns the array in a random order; can be called in list context or scalar context and will return an arrayref in the latter case.

=head2 C<< shuffle_in_place() >>

Rearranges the array in a random order, and changes the attribute to point to the new order.

=head2 C<< sort( $coderef? ) >>

Arguments: B<< Optional[CodeRef] >>.

Like C<sort> from L<perlfunc>.

=head2 C<< sort_in_place( $coderef? ) >>

Arguments: B<< Optional[CodeRef] >>.

Like C<sort> from L<perlfunc>, but changes the attribute to point to the newly sorted array.

=head2 C<< splice( $index, $length, @values ) >>

Like C<splice> from L<perlfunc>.

=head2 C<< sum() >>

Like C<< List::Util::sum0() >>.

=head2 C<< tail( $count ) >>

Arguments: B<< Int >>.

Returns the last C<< $count >> elements of the array in list context.

=head2 C<< uniq() >>

Returns the array filtered to remove duplicates; can be called in list context or scalar context and will return an arrayref in the latter case.

=head2 C<< uniq_in_place() >>

Filters the array to remove duplicates, and changes the attribute to point to the filtered array.

=head2 C<< uniqnum() >>

Returns the array filtered to remove duplicates numerically; can be called in list context or scalar context and will return an arrayref in the latter case.

=head2 C<< uniqnum_in_place() >>

Filters the array to remove duplicates numerically, and changes the attribute to point to the filtered array.

=head2 C<< uniqstr() >>

Returns the array filtered to remove duplicates stringwise; can be called in list context or scalar context and will return an arrayref in the latter case.

=head2 C<< uniqstr_in_place() >>

Filters the array to remove duplicates stringwise, and changes the attribute to point to the filtered array.

=head2 C<< unshift( @values ) >>

Adds an element to the start of the array.

  my $object = My::Class->new( attr => [ 'foo' ] );
  $object->my_unshift( 'bar', 'baz' );
  say Dumper( $object->attr ); ## ==> [ 'bar', 'baz', 'foo' ]

=head1 SHORTCUT CONSTANTS

This module provides some shortcut constants for indicating a list of
delegations.

  package My::Class {
    use Moo;
    use Sub::HandlesVia;
    use Sub::HandlesVia::HandlerLibrary::Array qw( HandleQueue );
    
    has things => (
      is          => 'ro',
      handles_via => 'Array',
      handles     => HandleQueue,
      default     => sub { [] },
    );
  }

These shortcuts can be combined using the C< | > operator.

    has things => (
      is          => 'ro',
      handles_via => 'Array',
      handles     => HandleQueue | HandleStack,
      default     => sub { [] },
    );

=head2 C<< HandleQueue >>

Creates delegations named like C<< things_is_empty >>, C<< things_size >>,
C<< things_enqueue >>, C<< things_dequeue >>, and C<< things_peek >>.

=head2 C<< HandleStack >>

Creates delegations named like C<< things_is_empty >>, C<< things_size >>,
C<< things_push >>, C<< things_pop >>, and C<< things_peek >>.

=head1 EXTENDED EXAMPLES

=head2 Using for_each

  use strict;
  use warnings;
  
  package My::Plugin {
    use Moo::Role;
    sub initialize {}
    sub finalize {}
  }
  
  package My::Processor {
    use Moo;
    use Sub::HandlesVia;
    use Types::Standard qw( ArrayRef ConsumerOf );
    
    has plugins => (
      is => 'ro',
      isa => ArrayRef[ ConsumerOf['My::Plugin'] ],
      handles_via => 'Array',
      handles => {
        add_plugin => 'push',
        plugin_do => 'for_each',
      },
      default => sub { [] },
    );
    
    sub _do_stuff {
      return;
    }
    
    sub run_process {
      my ( $self, @args ) = @_;
      $self->plugin_do( sub {
        my $plugin = shift;
        $plugin->initialize( $self, @args );
      } );
      $self->_do_stuff( @args );
      $self->plugin_do( sub {
        my $plugin = shift;
        $plugin->finalize( $self, @args );
      } );
    }
  }
  
  my $p = My::Processor->new();
  
  package My::Plugin::Noisy {
    use Moo; with 'My::Plugin';
    sub initialize {
      my ( $self, $processor, @args ) = @_;
      say "initialize @args"; ## ==> 'initialize 1 2 3'
    }
    sub finalize {
      my ( $self, $processor, @args ) = @_;
      say "finalize @args"; ## ==> 'finalize 1 2 3'
    }
  }
  
  $p->add_plugin( My::Plugin::Noisy->new );
  
  $p->run_process( 1, 2, 3 );

=head2 Job queue using push and shift

  use strict;
  use warnings;
  use Try::Tiny;
  
  package My::JobQueue {
    use Moo;
    use Sub::HandlesVia;
    use Types::Standard qw( Bool ArrayRef CodeRef HasMethods is_Object );
    use Try::Tiny;
    
    has auto_requeue => (
      is => 'ro',
      isa => Bool,
      default => 0,
    );
    
    has jobs => (
      is => 'ro',
      isa => ArrayRef[ CodeRef | HasMethods['run'] ],
      handles_via => 'Array',
      handles => {
        add_job => 'push',
        _get_job => 'shift',
        is_empty => 'is_empty',
      },
      default => sub { [] },
    );
    
    sub _handle_failed_job {
      my ( $self, $job ) = @_;
      $self->add_job( $job ) if $self->auto_requeue;
    }
    
    sub run_jobs {
      my $self = shift;
      while ( not $self->is_empty ) {
        my $job = $self->_get_job;
        try {
          is_Object($job) ? $job->run() : $job->();
        }
        catch {
          $self->_handle_failed_job( $job );
        };
      }
    }
  }
  
  my $q = My::JobQueue->new();
  
  my $str = '';
  $q->add_job( sub { $str .= 'A' } );
  $q->add_job( sub { $str .= 'B' } );
  $q->add_job( sub { $str .= 'C' } );
  
  $q->run_jobs;
  
  say $str; ## ==> 'ABC'
  
  # Attempt to push invalid value on the queue
  #
  try {
    $q->add_job( "jobs cannot be strings" );
  }
  catch {
    say $q->is_empty;  ## ==> true
  };

=head1 BUGS

Please report any bugs to
L<https://github.com/tobyink/p5-sub-handlesvia/issues>.

=head1 SEE ALSO

L<Sub::HandlesVia>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2020, 2022 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.