Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
/ List-Bisect Public archive

Split a list in to two or three parts based on a block, much like a grep that keeps all values.

Notifications You must be signed in to change notification settings

notbenh/List-Bisect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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]

About

Split a list in to two or three parts based on a block, much like a grep that keeps all values.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages