Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created September 24, 2021 07:44
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 gfldex/55388791a14ec4066282903f8c2a1401 to your computer and use it in GitHub Desktop.
Save gfldex/55388791a14ec4066282903f8c2a1401 to your computer and use it in GitHub Desktop.
use v6.d;
sub consecutive-a(*@a) {
my @ret;
gather {
for (|@a, |@a.tail).rotor( 2 => -1 ) -> [$a, $b] {
@ret.push: $a;
unless $b == $a + 1 {
take @ret;
@ret = [];
}
}
}
}
sub consecutive-b(*@a) {
my @gaps = @a.rotor(2 => -1).kv.grep(-> $index, [$a, $b] { $b !== $a + 1 })[*;0];
return @a unless @gaps;
@gaps = (@gaps Z @gaps »+» 1).flat;
my $ranges := (0, @gaps, (@a - 1)).flat.map(-> \l, \r { l .. r });
@a[$ranges]
}
sub MAIN() {
my @examples := (1, 2, 3, 6, 7, 8, 9)
,(11, 12, 14, 17, 18, 19)
,(2, 4, 6, 8)
,(1, 2, 3, 4, 5);
.&consecutive-a.say for @examples;
say();
.&consecutive-b.say for @examples;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment