List::Conditional - Create lists based on a condition for each element


List-Conditional documentation Contained in the List-Conditional distribution.

Index


Code Index:

NAME

Top

List::Conditional - Create lists based on a condition for each element

VERSION

Top

Version 0.02

SYNOPSIS

Top

    use List::Conditional;

	# academic example
	my @list = clist(1 => 'a', 0 => 'b', 1 => 'c');
	# same as @list = ('a', 'c');

    # a more practical example
	my @things_to_pack = clist(
		    $destination->is_rainy() => 'umbrella',
		    $destination->is_warm()  => 'shorts',
		    $myself->is_sick()       => 'meds',
		    $flight->duration() > 2  => 'pillow',
		    int $myself->children()  => 'toys',
		    ...
	);

	


EXPORT

Top

clist is automatically exported, as it is the only function in this module.

FUNCTIONS

Top

clist

Arguments: an even number of elements, interpreted as pairs of <condition = value>>

Returns: the list of values, for which the associated condition evaluates to true.

This function provides a nice functional and highly readable approach to conditional list building, instead of using imperative control structures like if and push or the ternary operator condition ? value : ().

Beware that all conditions and values are passed to clist in list context, and therefore explicitly enforce scalar context for your conditions and values, as seen in the "SYNOPSIS".

If you want multiple values per condition, you can use List::Flatten as follows:

	use List::Flatten;

	my @things_to_pack = flat clist(
		    $destination->is_rainy() => 'umbrella',
		    $destination->is_warm()  => ['shorts', 'sandals'],
		    $myself->is_sick()       => 'meds',
		    $flight->duration() > 2  => 'pillow',
		    int $myself->children()  => ['toys', 'candy'],
		    ...
	);

If you need alternative values in case the conditions do not hold, then the ternary operator is right for you, not this function.

AUTHOR

Top

Darko Obradovic, <dobradovic at gmx.de>

BUGS

Top

Please report any bugs or feature requests to bug-list-conditional at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Conditional. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc List::Conditional




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=List-Conditional

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/List-Conditional

* CPAN Ratings

http://cpanratings.perl.org/d/List-Conditional

* Search CPAN

http://search.cpan.org/dist/List-Conditional

COPYRIGHT & LICENSE

Top


List-Conditional documentation Contained in the List-Conditional distribution.
package List::Conditional;

use warnings;
use strict;

require Exporter;
use base qw(Exporter);
our @EXPORT = qw(clist);

use Carp qw(croak);
use List::MoreUtils qw(natatime);


our $VERSION = '0.02';


sub clist {
	croak "odd number of elements passed to clist, requires (condition => value)-pairs!" if @_ % 2;
	my @result = ();
	my $it = natatime 2, @_;
	while (my ($condition, $element) = $it->()) {
		push @result, $element if $condition;
	}
	return @result;
}


1;