Util-Medley

 view release on metacpan or  search on metacpan

lib/Util/Medley/List.pm  view on Meta::CPAN

package Util::Medley::List;
$Util::Medley::List::VERSION = '0.062';
#########################################################################################

use v5.16;
use Modern::Perl;
use Moose;
use namespace::autoclean;
use Kavorka '-all';
use Data::Printer alias => 'pdump';
use List::Util;
use List::Compare;
use Sort::Naturally;

=head1 NAME

Util::Medley::List - utility methods for working with lists

=head1 VERSION

version 0.062

=cut

=head1 SYNOPSIS

 %map = $util->listToMap(@list);
 %map = $util->listToMap(list => \@list);
 
 $min = $util->min(@list);
 $min = $util->min(list => \@list);
 
 $max = $util->max(@list);
 $max = $util->max(list => \@list);

 @list = $util->undefsToStrings(@list);
 @list = $util->undefsToStrings(list => \@list);

 @uniq = $util->uniq@list);
 @uniq = $util->uniq(list => \@list); 
 
=head1 DESCRIPTION

...

=cut

#########################################################################################

=head1 METHODS

=head2 contains

Search for pattern in a list.

Returns bool

=over

=item usage:

  $bool = $util->contains(\@list, 'mystring');
  $bool = $util->contains(\@list, undef);
  $bool = $util->contains(\@list, qr/myregex/);

  $bool = $util->contains(list => \@list, match => 'mystring');
  $bool = $util->contains(list => \@list, match => undef);
  $bool = $util->contains(list => \@list, match => qr/myregex/);;
   
=item args:

=over

lib/Util/Medley/List.pm  view on Meta::CPAN


	return $self->listToMap(@$list);
}

=head1 min

Just a passthrough to List::Util::min()

=over

=item usage:

 $min = $util->min(@list);
 
 $min = $util->min(list => \@list);

=back
 
=cut

multi method min (@list) {

    return List::Util::min(@list);    
}

multi method min (ArrayRef :$list!) {

	return $self->min(@$list);	
}

=head1 max

Just a passthrough to List::Util::max()

=over

=item usage:

 $max = $util->max(@list);
 
 $max = $util->max(list => \@list);

=back
 
=cut

multi method max (@list) {

    return List::Util::max(@list);    
}

multi method max (ArrayRef :$list!) {

	return $self->max(@$list);	
}


=head1 nsort 

Sort an array naturally (case in-sensitive).  This behaves the same way
Sort::Naturally::nsort does with the exception of using fc (casefolding) 
instead of lc (lowercase).  This guarantees the same results without
regard to locale (which Sort::Naturally::nsort does use).

=over

=item usage:

 @sorted = $util->nsort(@list);
 
 @sorted = $util->nsort(list => \@list);

=item args:

=over

=item list [Array|ArrayRef]

The list to act on.

=back

=back
 
=cut

multi method nsort (ArrayRef :$list!) {

	#
	# parse each element into an arrayref splitting by number
	#
	my @bits;
	foreach my $el (@$list) {

		my $x   = defined($el) ? $el : '';
		my @bit = ($x);

		if ( $x =~ m/^[+-]?(?=\d|\.\d)\d*(?:\.\d*)?(?:[Ee](?:[+-]?\d+))?\z/s ) {

			# pure numeric
			push @bit, '', $x;
		}
		else {
			# Consume the string.
			while ( length $x ) {
				push @bit, ( $x =~ s/^(\D+)//s ) ? CORE::fc($1) : '';
				push @bit, ( $x =~ s/^(\d+)//s ) ? $1     : 0;
			}
		}

		push @bits, \@bit;
	}

	#
	# sort each element by its parsed bits
	#
	my @sorted_bits = sort {

		# Uses $i as the index variable, $x as the result.
		my $x;
		my $i = 1;

		while ( $i < @$a and $i < @$b ) {



( run in 0.865 second using v1.01-cache-2.11-cpan-ff9377addf4 )