Re: Variable character class

2019-09-02 Thread Joseph Brenner
>   The "implicit" alternation comes from interpolating a list (of subrules,
> see below).

I see.  And that's discussed here (had to really look for it):

https://docs.perl6.org/language/regexes#Quoted_lists_are_LTM_matches

At first I was looking further down in the "Regex interpolation"
section, where it's also touched on, though I kept missing it:

> When an array variable is interpolated into a regex, the regex engine handles 
> it like a | alternative of the regex elements (see the documentation on 
> embedded lists, above).


On 9/1/19, The Sidhekin  wrote:
> On Mon, Sep 2, 2019 at 1:12 AM Joseph Brenner  wrote:
>
>> I was just trying to run Simon Proctor's solution, and I see it
>> working for Yary's first case, but not his more complex one with
>> problem characters like brackets included in the list of characters.
>>
>> I don't really see how to fix it, in part because I'm not that
>> clear on what it's actually doing... there's some sort of
>> implicit alternation going on?
>>
>>
>> sub contains( Str $chars, Str $_ ) {
>>   m:g/<{$chars.comb}>+/
>> };
>>
>
>   The "implicit" alternation comes from interpolating a list (of subrules,
> see below).
>
> That works for this case:
>>
>>   say contains('24680', '19584203');
>>   # (「8420」)
>>
>> But on something like this it errors out:
>>
>>   say contains('+\/\]\[', 'Apple ][+//e'); # says ][+//
>>
>
>   … because it's trying to compile each (1-character) string as a subrule …
>
>   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+/
> }
>
>
>   (… hey, is there a word for "block wrapped in angle brackets"?)
>
>
> Eirik
>


Re: Variable character class

2019-09-02 Thread Gianni Ceccarelli
On 2019-09-02 The Sidhekin  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 - 
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)


Re: Variable character class

2019-09-02 Thread Paul Procacci
I don't know then.

I've created the following ticket:

https://github.com/perl6/doc/issues/2999

Feel free to place your own input there if you feel the need.

On Mon, Sep 2, 2019 at 12:37 PM William Michels 
wrote:

> Sorry Paul, I don't get the correct answer in any of the three cases I
> tried. Here's what 6Pad returns:
>
> https://perl6.github.io/6pad/
>
> sub matching_chars(Str $chars_to_match, Str $str) {
> # warnings, treats as string not variable
> $str ~~ /<$_>/ given "<[$chars_to_match]>";
> }
>
> say matching_chars("24680", "19584203"); # expect 「8420」
> say matching_chars("Lorem ipsum dolor sit amet, consectetuer
> adipiscing elit.", "abcdef"); # expect 「a」 「cde」
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # expect 「][+//」
>
> 「19584203」
> 「abcdef」
> ===SORRY!=== Error while compiling ?/EVAL_7 Malformed regex at
> ?/EVAL_7:1 --> anon regex { Apple ][+//e} expecting any of: infix
> stopper
>
> HTH, Bill.
>
>
> On Mon, Sep 2, 2019 at 7:54 AM Paul Procacci  wrote:
> >
> > Was talking to folks over on the #perl6 IRC channel.
> > It appears the recommended way is:
> >
> > sub matching_chars(Str $chars_to_match, Str $str) {
> > # warnings, treats as string not variable
> > $str ~~ /<$_>/ given "<[$chars_to_match]>";
> > }
> >
> > ~Paul
> >
> >
> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
> >>
> >> I found something easy in Perl 5 that's puzzling me in Perl 6-
> specifying a character class via a variable.
> >>
> >> Perl 5:
> >> sub matching_chars {
> >>   (my $chars_to_match, local $_) = @_;
> >>   /([$chars_to_match]+)/
> >> }
> >>
> >> say matching_chars('24680', '19584203'); # says 8420
> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
> >>
> >> Perl 6:
> >> sub matching_chars(Str $chars_to_match, Str $_) {
> >> # warnings, treats as string not variable
> >> m/<[$chars_to_match]>/;
> >> }
> >>
> >> How do I get Perl 6 to interpret a variable in the contents of a
> character class?
> >> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
> think that  Rakudo would use the literal contents of $chars_to_match,
> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
> warning about repeated c, underscore, etc.
> >>
> >> -y
> >
> >
> >
> > --
> > __
> >
> > :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Variable character class

2019-09-02 Thread William Michels via perl6-users
Sorry Paul, I don't get the correct answer in any of the three cases I
tried. Here's what 6Pad returns:

https://perl6.github.io/6pad/

sub matching_chars(Str $chars_to_match, Str $str) {
# warnings, treats as string not variable
$str ~~ /<$_>/ given "<[$chars_to_match]>";
}

say matching_chars("24680", "19584203"); # expect 「8420」
say matching_chars("Lorem ipsum dolor sit amet, consectetuer
adipiscing elit.", "abcdef"); # expect 「a」 「cde」
say matching_chars('+\/\]\[', 'Apple ][+//e'); # expect 「][+//」

「19584203」
「abcdef」
===SORRY!=== Error while compiling ?/EVAL_7 Malformed regex at
?/EVAL_7:1 --> anon regex { Apple ][+//e} expecting any of: infix
stopper

HTH, Bill.


On Mon, Sep 2, 2019 at 7:54 AM Paul Procacci  wrote:
>
> Was talking to folks over on the #perl6 IRC channel.
> It appears the recommended way is:
>
> sub matching_chars(Str $chars_to_match, Str $str) {
> # warnings, treats as string not variable
> $str ~~ /<$_>/ given "<[$chars_to_match]>";
> }
>
> ~Paul
>
>
> On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>>
>> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying a 
>> character class via a variable.
>>
>> Perl 5:
>> sub matching_chars {
>>   (my $chars_to_match, local $_) = @_;
>>   /([$chars_to_match]+)/
>> }
>>
>> say matching_chars('24680', '19584203'); # says 8420
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>>
>> Perl 6:
>> sub matching_chars(Str $chars_to_match, Str $_) {
>> # warnings, treats as string not variable
>> m/<[$chars_to_match]>/;
>> }
>>
>> How do I get Perl 6 to interpret a variable in the contents of a character 
>> class?
>> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think 
>> that  Rakudo would use the literal contents of $chars_to_match, instead it's 
>> using the literal chars "$ c h a r s _ t o _ m a t c h" and warning about 
>> repeated c, underscore, etc.
>>
>> -y
>
>
>
> --
> __
>
> :(){ :|:& };:


Re: Variable character class

2019-09-02 Thread Paul Procacci
Was talking to folks over on the #perl6 IRC channel.
It appears the recommended way is:

sub matching_chars(Str $chars_to_match, Str $str) {
# warnings, treats as string not variable
$str ~~ /<$_>/ given "<[$chars_to_match]>";
}

~Paul


On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:

> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying
> a character class via a variable.
>
> Perl 5:
> sub matching_chars {
>   (my $chars_to_match, local $_) = @_;
>   /([$chars_to_match]+)/
> }
>
> say matching_chars('24680', '19584203'); # says 8420
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>
> Perl 6:
> sub matching_chars(Str $chars_to_match, Str $_) {
> # warnings, treats as string not variable
> m/<[$chars_to_match]>/;
> }
>
> How do I get Perl 6 to interpret a variable in the contents of a character
> class?
> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think
> that  Rakudo would use the literal contents of $chars_to_match, instead
> it's using the literal chars "$ c h a r s _ t o _ m a t c h" and warning
> about repeated c, underscore, etc.
>
> -y
>


-- 
__

:(){ :|:& };:


Re: Variable character class

2019-09-02 Thread William Michels via perl6-users
Hi Aureliano!

Others will certainly have a more informed answer, but I think it
might be because:

1). Set operations in general in Perl6 (on characters, etc.) may use a
unordered matching algorithm to enhance speed, and/or
2). I didn't explicitly call a "Sort" method, function or routine in
matching the characters, and/or
3). The resulting character order (probably represented internally as
Unicode codepoints), may indeed have some sort of order to them, but
that order/pseudo-order is either A. related to their initial position
in the input string (0,1,2,3...), or B. that order/pseudo-order is
related to each character's numeric representation in Unicode.

Below I convert the code to accept two integers instead of two
strings. I set up a few different "return" lines for you to check out.
You will see that the (second) "return" line that calls ".sort" as the
last method indeed returns a set of integers in numerical order! But
it also looks as though the (first) return line without an explicit
".sort" call ALSO returns the Intersection Set in numerical order,
"(set(0 2 4 6 8))".

my Int $u = 24680;
my @s = $u.comb.unique;
say @s ; # [2 4 6 8 0]

sub matching_chars(Int $a, Int $b) {
   my @c = $a.comb.unique;
   my @d = $b.comb.unique;
   #return @c (&) @d; # Returns Intersection Set (Ints)
   #return [@c (&) @d].sort; # Returns Sorted Set (Ints)
   return [@c (&) @d].sort.Str; # Returns varying order (Strs)
}

say matching_chars(24680, 1234567890); # RETURNS (set(0 2 4 6 8))


So I think Perl6 may have fast integer matching/sorting algorithms
built-in, but the built-in matching/sorting algorithms for characters
may be somewhat different, different enough to require a
explicit/final (e.g. alphabetic) sort, if that's what the programmer
desires.

HTH, Bill.






On Sun, Sep 1, 2019 at 10:10 PM Aureliano Guedes
 wrote:
>
> Why does it dont return ordered for neither of those lists?
> I mean:
>
> my $u = "24680";
> my @s = $u.comb.unique;
> say @s ; # [2 4 6 8 0]
>
> sub matching_chars(Str $a, Str $b) {
>my @c = $a.comb.unique;
>my @d = $b.comb.unique;
>return ~[@c (&) @d];
> }
>
> say matching_chars("24680", "1234567890"); # says 2 0 8 4
>
> On Mon, Sep 2, 2019 at 1:20 AM William Michels via perl6-users 
>  wrote:
>>
>> Thanks Simon, good point. I ran into the same trouble as others trying
>> to get the answer via regex, and switched over to sets as an
>> alternative. I'll confess I completely missed that Yary's Perl5 code
>> returned the substring "8420" present in his test "19584203" string,
>> and that was the answer he was going for.
>>
>> Simon and Eirik, I still think there might be some value in calling
>> the .unique method on $chars.comb as in "$chars.comb.unique". But I
>> haven't had the opportunity to check code efficiency  +/-  the
>> ".unique" method call.
>>
>> For those interested in using Sets as a solution, you can stringifying
>> the 'return' line (below). Still working on using "join()" to
>> concatenate!
>>
>> sub matching_chars(Str $a, Str $b) {
>>my @c = $a.comb.unique;
>>my @d = $b.comb.unique;
>>#return (~[@c (&) @d]).^name;
>>return ~[@c (&) @d];
>> }
>>
>> say matching_chars("24680", "19584203"); # says 2 0 8 4
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says [ + ] /
>> say matching_chars("Lorem ipsum dolor sit amet, consectetuer
>> adipiscing elit.", "abcdef"); # says c a d e
>>
>>
>> HTH, Bill.
>>
>> PS. Simon, thanks for all the great Youtube videos you've done from LondonPM!
>>
>> https://youtu.be/yt8SrZ_V_50
>> https://youtu.be/9M1xZQ0_Skw
>>
>>
>>
>>
>>
>>
>>
>>
>> On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor  wrote:
>> >
>> > Using a set would be good but it doesn't give you the matching string from 
>> > the original (which is what I thought was required) otherwise Sets would 
>> > be my first thought.
>> >
>> > On Sun, 1 Sep 2019, 17:57 William Michels,  wrote:
>> >>
>> >> Hi Yary and Paul and Simon,
>> >>
>> >> I ran into the same difficulties as Yary with repeated characters, so
>> >> I tried the .unique method. Then after a while, I realized that
>> >> problems like this might best be treated as "Set" problems in Perl6.
>> >> Note the Set Intersection operator "(&)" below:
>> >>
>> >> sub matching_chars(Str $a, Str $b) {
>> >>my @c = $a.comb.unique;
>> >>my @d = $b.comb.unique;
>> >>#say @c; say @d;
>> >>return @c (&) @d;
>> >> }
>> >>
>> >> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
>> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
>> >>
>> >>
>> >> https://docs.perl6.org/routine/Set
>> >> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
>> >>
>> >> HTH, Bill.
>> >>
>> >>
>> >> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci  wrote:
>> >> >
>> >> > I'm not entirely sure if this is the correct answer, but if you define 
>> >> > your own custom character class
>> >> > with a 'regex' object, you can use that in the grouping.
>> >> >
>> >> > sub matching_chars(Str