Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active January 28, 2021 03:28
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 0racle/72afb15114b08f035962cde8a7b8e121 to your computer and use it in GitHub Desktop.
Save 0racle/72afb15114b08f035962cde8a7b8e121 to your computer and use it in GitHub Desktop.
M×N Grepper
sub mngrepper($n, $m=$n) {
    sub (@xs, &f) {
        (^$n X ^$m).grep(-> ($y, $x) { f(@xs[$x;$y]) })
    }
}

my @puzzle = [<a b>], [<c d>];
my @visited = (True, False), ((Any), False);

for mngrepper(2)(@visited, ?*) -> ($y, $x) {
    @puzzle[$x;$y] ~= '*'
}
say @puzzle;

# or save your n×m grepper for easy re-use

my &mngrep = mngrepper(4, 2);

my @xs = [<a b c d>], [<e f g h*>];
my @indices = mngrep(@xs, *.contains: '*');

for @indices -> ($y, $x) {
    @xs[$x;$y] .= chop
}
say @xs;

UPDATE

Just use Deepgrep

for @xs.&deepgrep(*.contains('*'), :k) -> ($x, $y) {
    @xs[$x;$y] .= chop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment