Created
January 23, 2017 02:57
-
-
Save samcv/6493f4a0c49fc7515b42482444708e92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use nqp; | |
my %ranges; | |
for 0..20 { | |
%ranges{$_} = '<control>'; | |
} | |
for 80.. 90 { | |
%ranges{$_} = '<control>'; | |
} | |
my $first; | |
my $last; | |
my @r_iter = %ranges.keys.sort(+*); | |
my $range-no = 0; | |
my %range-nos; | |
sub set-range ( Cool:D $point ) { | |
say "see point $point"; | |
# If a first point doesn't exist, this is the first point of a new range | |
if ! %range-nos { | |
%range-nos{$range-no}<first> = $point; | |
%range-nos{$range-no}<last> = $point; | |
} | |
elsif ! %range-nos{$range-no}:exists { | |
%range-nos{$range-no}<first> = $point; | |
%range-nos{$range-no}<last> = $point; | |
} | |
# if the points are just one off we're part of the same range | |
elsif %range-nos{$range-no}<last> + 1 == $point { | |
%range-nos{$range-no}<last> = $point; | |
} | |
# Otherwise it's part of a new range | |
else { | |
%range-nos{++$range-no}<first> = $point | |
} | |
} | |
for %ranges.keys.sort(+*) { | |
set-range $_; | |
} | |
say %range-nos.perl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment