| 1 | package List::Bisect; | ||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | $List::Bisect::VERSION = '0.002'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Exporter qw{import}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | our @EXPORT = qw{bisect trisect}; | |||||||||||||
| 9 | |||||||||||||||
| 10 | # ABSTRACT: split a list in to two parts by way of a grep like block | ||||||||||||||
| 11 | |||||||||||||||
| 12 | =head1 EXPORTED FUNCTION | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head2 bisect | ||||||||||||||
| 15 | |||||||||||||||
| 16 | my ($a,$b) = bisect {$_ <= 5} 1..10; | ||||||||||||||
| 17 | # $a == [1..5] | ||||||||||||||
| 18 | # $b == [6..10] | ||||||||||||||
| 19 | |||||||||||||||
| 20 | Useage is like grep where you pass it a block and a list, returns a list of two arrayrefs. All | ||||||||||||||
| 21 | TRUE values are put in to the first arrayref, FALSE in the second arrayref. | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =cut | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 1 | ▶ | sub bisect (&@) { | ||||||||||||
|
Subroutine
bisect: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 26 | 1 | my $grep = shift; | |||||||||||||
| 27 | |||||||||||||||
| 28 | 1 | my @true; | |||||||||||||
| 29 | my @false; | ||||||||||||||
| 30 | 1 | foreach $_ (@_) { | |||||||||||||
| 31 | 10 | ▶ | if (&$grep) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | 5 | push @true, $_; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | else { | ||||||||||||||
| 35 | 5 | push @false, $_; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 1 | return (\@true,\@false); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =head2 trisect | ||||||||||||||
| 43 | |||||||||||||||
| 44 | my ($a,$b,$c) = trisect {$_ <=> 5} 1..10; | ||||||||||||||
| 45 | # $a == [1..4] | ||||||||||||||
| 46 | # $b == [5] | ||||||||||||||
| 47 | # $c == [6..10] | ||||||||||||||
| 48 | |||||||||||||||
| 49 | Useage is like grep where you pass it a block and a list, returns a list of three arrayrefs. The | ||||||||||||||
| 50 | intent here though is to break that list in to three parts using cmp-style returns (-1/0/1). All | ||||||||||||||
| 51 | values that cause your codeblock to return -1 are in the first arrayref, 0 in the next, and | ||||||||||||||
| 52 | everything else falls in the last arrayref. | ||||||||||||||
| 53 | |||||||||||||||
| 54 | B<!!NOTE!!> Currently the last arrayref is a catch all for anything that does not exactly match | ||||||||||||||
| 55 | -1/0. If you write your own cusom block that returns any value other then -1/0/1 then it will | ||||||||||||||
| 56 | end up here. This was done as I want to keep the expectation that all items from the input list | ||||||||||||||
| 57 | will be found some where in the output. | ||||||||||||||
| 58 | |||||||||||||||
| 59 | my ($x,$y,$z) = trisect { $_ < 5 ? -1 | ||||||||||||||
| 60 | : $_ > 5 ? 1 | ||||||||||||||
| 61 | : 'foo' | ||||||||||||||
| 62 | } 1..10; | ||||||||||||||
| 63 | # $x == [1..4] | ||||||||||||||
| 64 | # $y == [] | ||||||||||||||
| 65 | # $z == [5..10] | ||||||||||||||
| 66 | |||||||||||||||
| 67 | =cut | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 2 | ▶ | sub trisect (&@) { | ||||||||||||
|
Subroutine
trisect: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 70 | 2 | require Scalar::Util; | |||||||||||||
| 71 | 2 | my $cmp = shift; | |||||||||||||
| 72 | |||||||||||||||
| 73 | 2 | my @lt; | |||||||||||||
| 74 | my @eq; | ||||||||||||||
| 75 | 2 | my @gt; | |||||||||||||
| 76 | 2 | foreach $_ (@_) { | |||||||||||||
| 77 | 20 | my $val = &$cmp; | |||||||||||||
| 78 | 20 | ▶ | if (Scalar::Util::looks_like_number($val) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | 19 | ▶ | if ($val == -1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 80 | 8 | push @lt, $_; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | elsif ($val == 0) { | ||||||||||||||
| 83 | 1 | push @eq, $_; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | else { | ||||||||||||||
| 86 | 10 | push @gt, $_; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | else { | ||||||||||||||
| 90 | 1 | push @gt, $_; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 2 | return (\@lt,\@eq,\@gt); | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | |||||||||||||||
| 98 | |||||||||||||||
| 99 | |||||||||||||||
| 100 | 1 | 1; | |||||||||||||