Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created April 25, 2016 18:11
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 zoffixznet/b88f63a20e4b0eb2bede859eb86b0c54 to your computer and use it in GitHub Desktop.
Save zoffixznet/b88f63a20e4b0eb2bede859eb86b0c54 to your computer and use it in GitHub Desktop.
Relevant IRC discussion: http://irclog.perlgeek.de/perl6/2016-04-25#i_12390649
I propose we add :captures argument to .comb sub/method that will modify the behavious to return a list of captures inside the given Matcher.
Consider this Perl 5 code:
my %h = 'moo=meow ping=pong' =~ /(\w+)=(\w+)/g
It constructs a hash with keys `moo` and `ping` whose values are `meow` and `pong` respectively. To replicate the same behaviour in Perl 6, the simplest code I came up with asks .comb for Match objects, which through a series of hypers and coersions, it flattens into a list I can assign to a hash:
my %h = 'moo=meow ping=pong'.comb(/(\w+) '=' (\w+)/, :match)>>.Slip.>>.Str
Not only is the code verbose, the coersions make it harder to understand that the goal is here. With my proposal in place, the above code will be simplified to:
my %h = 'moo=meow ping=pong'.comb: /(\w+) '=' (\w+)/, :captures;
Another example could be picking data from text:
my @rows = $ascii-table.comb: /^^ \s* (.?) \s* '|' \s* (.?) \s* '|' \s* (.?) \s* $$/, :captures;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment