Skip to content

Instantly share code, notes, and snippets.

@samcv
Created January 23, 2017 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samcv/6493f4a0c49fc7515b42482444708e92 to your computer and use it in GitHub Desktop.
Save samcv/6493f4a0c49fc7515b42482444708e92 to your computer and use it in GitHub Desktop.
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