NAME

Role::TinyCommons::Collection::PickItems - The pick_items() interface

VERSION

This document describes version 0.009 of Role::TinyCommons::Collection::PickItems (from Perl distribution Role-TinyCommons-Collection), released on 2023-08-26.

SYNOPSIS

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;

DESCRIPTION

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.

REQUIRED METHODS

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.

PROVIDED METHODS

pick_item

Usage:

my $item = $obj->pick_item(%args);

Equivalent to:

my @items = $obj->pick_items(n => 1, %args);
return @items ? $items[0] : undef;

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Role-TinyCommons-Collection.

SOURCE

Source repository is at https://github.com/perlancar/perl-Role-TinyCommons-Collection.

SEE ALSO

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTING

To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

% prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 2023, 2021 by perlancar <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.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Role-TinyCommons-Collection

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.