On 2019-09-02 The Sidhekin <sidhe...@gmail.com> wrote:
>   To have the (1-character) strings used a literals, rather than
> compiled as subrules, put them in an array instead of a block wrapped
> in angle brackets:
> 
> sub contains( Str $chars, Str $_ ) {
>   my @arr = $chars.comb;
>   m:g/@arr+/

This looks to be the correct solution. Notice that yary's initial code
had::

  matching_chars('+\/\]\[', 'Apple ][+//e')

but those backslashes are an implementation artefact (the Perl5
version interpolates the given string into a regex without calling
``quotemeta``).

If we remove the backslashes, Sidhekin's code works::

  #!perl6
  use v6;
  use Test;

  sub matching_chars(Str $chars_to_match, Str $target) {
    my @chars-to-match = $chars_to_match.comb;
    return $target ~~ m:g/@chars-to-match + /;
  }

  is matching_chars("24680", "19584203"),['8420'];
  is matching_chars("Lorem ipsum dolor sit amet, consectetueradipiscing elit.",
                    "abcdef"), ['a','cde'];
  is matching_chars('+/][', 'Apple ][+//e'),['][+//'];
  is matching_chars('24680', '19584203'),['8420'];
  is matching_chars('24680', '24680x'),['24680'];
  is matching_chars('246','13 91 1462 7'),['462'];

  done-testing;

-- 
        Dakkar - <Mobilis in mobile>
        GPG public key fingerprint = A071 E618 DD2C 5901 9574
                                     6FE2 40EA 9883 7519 3F88
                            key id = 0x75193F88

Just because it's not nice doesn't mean it's not miraculous.
        -- (Terry Pratchett, Interesting Times)

Reply via email to