NAME

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

VERSION

This document describes version 0.003 of Role::TinyCommons::Collection::PickItems (from Perl distribution Role-TinyCommons-Collection), released on 2021-04-20.

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.

BUGS

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.

SEE ALSO

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

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.