Created
September 24, 2021 07:44
-
-
Save gfldex/55388791a14ec4066282903f8c2a1401 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 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