ben hengst > List-Bisect > List::Bisect

Download:
List-Bisect-0.002.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.002   Source  

EXPORTED FUNCTION ^

bisect

  my ($a,$b) = bisect {$_ <= 5} 1..10;
  # $a == [1..5]
  # $b == [6..10]

Useage is like grep where you pass it a block and a list, returns a list of two arrayrefs. All TRUE values are put in to the first arrayref, FALSE in the second arrayref.

trisect

  my ($a,$b,$c) = trisect {$_ <=> 5} 1..10;
  # $a == [1..4]
  # $b == [5]
  # $c == [6..10]

Useage is like grep where you pass it a block and a list, returns a list of three arrayrefs. The intent here though is to break that list in to three parts using cmp-style returns (-1/0/1). All values that cause your codeblock to return -1 are in the first arrayref, 0 in the next, and everything else falls in the last arrayref.

!!NOTE!! Currently the last arrayref is a catch all for anything that does not exactly match -1/0. If you write your own cusom block that returns any value other then -1/0/1 then it will end up here. This was done as I want to keep the expectation that all items from the input list will be found some where in the output.

  my ($x,$y,$z) = trisect { $_ < 5 ? -1
                          : $_ > 5 ? 1
                          : 'foo'
                          } 1..10;
  # $x == [1..4]
  # $y == []
  # $z == [5..10]
syntax highlighting: