Create a gist now

Instantly share code, notes, and snippets.

Embed
* ~~ /<$match> vs -> $x { $x ~~ /<$match>/ }
my @files = <foo bar baz>;
my @match-eq = <foo bar baz>.map( -> $s { * eq $s } ); # OK
my @match-rx = <foo ^ba>.map( -> $r { * ~~ /<$r>/ } ); # NOT OK
my @match-rx-pointy = <foo ^ba>.map( -> $r { -> $x { $x ~~ /<$r>/ } } ); # OK
sub match-it (@files, @matchers) {
return gather for @files -> $f {
take $f if @matchers.grep({ $_($f) });
}
}
say match-it(@files, @match-eq);
# output: (foo bar baz)
say match-it(@files, @match-rx);
# output: (bar baz)
# only matches the last matcher of the list,
# i.e. using <^ba foo> will output (foo)
say match-it(@files, @match-rx-pointy);
# output: (foo bar baz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment