Role::TinyCommons::Collection::PickItems - The pick_items() interface
This document describes version 0.003 of Role::TinyCommons::Collection::PickItems (from Perl distribution Role-TinyCommons-Collection), released on 2021-04-20.
In your class:
package YourClass; use Role::Tiny::With; with 'Role::TinyCommons::Collection::PickItems'; sub new { ... } sub pick_items { ... } ... 1;
In the code of your class user:
use YourClass; my $obj = YourClass->new(...); # pick 5 random items, without duplicates (but duplicate values are still possible) my @items = $obj->pick_items(n=>5); # pick 5 random items, duplicates allowed my @items = $obj->pick_items(n=>5, allow_resampling=>1); # pick a single random item, or undef if item is empty my $item = $obj->pick_item;
pick_items() is an interface to get one or more random items from a collection. Some options are provided. The implementor is given flexibility to support additional options, but the basic modes of picking must be supported.
pick_items()
Usage:
my @items = $obj->pick_items(%args);
Pick one or more random items from a collection. By default picks one item ("n"=1).
Arguments:
n
Type: posint (positive integer). Pick this many random items from the collection. By default resampling is not allowed. If there are only less than n items in the collection, then only that number of items should be returned.
This argument must be supported.
allow_resampling
Type: bool. Defaults to false. If set to false, then resampling is not allowed. Otherwise, resampling is allowed resulting in possible duplicates in the result.
This argument is optional to implement.
my $item = $obj->pick_item(%args);
Equivalent to:
my @items = $obj->pick_items(n => 1, %args); return @items ? $items[0] : undef;
Please visit the project's homepage at https://metacpan.org/release/Role-TinyCommons-Collection.
Source repository is at https://github.com/perlancar/perl-Role-TinyCommons-Collection.
Please report any bugs or feature requests on the bugtracker website https://github.com/perlancar/perl-Role-TinyCommons-Collection/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
perlancar <perlancar@cpan.org>
This software is copyright (c) 2021 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Role::TinyCommons::Collection, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Role::TinyCommons::Collection
CPAN shell
perl -MCPAN -e shell install Role::TinyCommons::Collection
For more information on module installation, please visit the detailed CPAN module installation guide.