That was just a dumb example.
An incredibly dumb example.

So what happens is that `Bool.pick` chooses The Bool values of either
`True` or `False`.
It does this at every position in the string.

    'TrueFalse' ~~ / <{ Bool.pick }>

Let's say that initially `Bool.pick` chooses `False`.
That value then gets temporarily inserted into the regex.
Obviously { "True" ne "False" } so it fails at the first position.

So then the regex tries to match at the second position.
`Bool.pick` gets run again, and picks one of the values.
Neither "True" nor "False" can match here so it doesn't matter which is
chosen.

It keeps doing that until it finally matches or reaches the end of the
string.

Let's take a look at what happens at the fifth position.
We obviously didn't match `True` at the start, because otherwise we would
get this far.
You may be expecting it to match `False` then.
The only thing is that the code inside of <{ }> gets run again.
This time it picks `True`.
{ "True" ne "False" } so it fails again.

It then keeps trying to find a match, only we've gotten so far into the
string that there are no matches left.
So then it just fails.

---

There is a 50% chance that it will return "True" if `Bool.pick` chooses
`True` the first time.
A 25% chance that it will return "False" if it picks `False` at the fifth
position.
And another 25% chance it will fail to match anything.

You can play around with the odds by changing the string your matching
against.
There will always be a chance that it will fail to match though.

On Sat, Jun 13, 2020 at 11:54 AM William Michels <w...@caa.columbia.edu>
wrote:

> Hi,
>
> I seem to be having some problems with Brad's code. The 'boolean pick'
> code seems far too clever for me to have intuited it on my own, so
> (to expand my Raku/Perl6 vocabulary), I played around with matching
> extra 'True' or 'False' values--as well as played around with seeing
> if similar 'boolean elems' code would work. However the code (even
> Brad's code) returned 'Nil' with alarming frequency (all code below
> tested in the Raku/Perl6 REPL):
>
> mbook:~ homedir$ perl6
> To exit type 'exit' or '^D'
> > 'TrueFalse' ~~ / <{ Bool.pick }> /
> 「False」
> > 'FalseTrueFalse' ~~ / <{ Bool.pick }> /
> 「False」
> > 'TrueFalseTrue' ~~ / <{ Bool.pick }> /
> Nil
> > 'TrueFalseTrue' ~~ / <{ Bool.elems }> /
> Nil
> > 'TrueFalse' ~~ / <{ Bool.elems }> /
> Nil
> > 'TrueFalse' ~~ / <{ Bool.pick }> /
> Nil
> > 'TrueFalse' ~~ / <{ Bool }> /
> Cannot resolve caller INTERPOLATE_ASSERTION(Match:D: Bool:U, BOOTInt,
> BOOTInt, BOOTInt, BOOTInt, PseudoStash:D); none of these signatures
> match:
>     (Match: Associative:D, $, $, $, $, $, *%_)
>     (Match: Iterable:D \var, int \im, int \monkey, int \s, $, \context,
> *%_)
>     (Match: Mu:D \var, int \im, int \monkey, $, $, \context, *%_)
>   in block <unit> at <unknown file> line 1
>
> > 'TrueFalse' ~~ / <{ Bool.say }> /
> (Bool)
> > say $*VM
> moar (2020.02.1)
> > exit
> mbook:~ homedir$ perl6 --version
> This is Rakudo version 2020.02.1.0000.1 built on MoarVM version 2020.02.1
> implementing Raku 6.d.
> mbook:~ homedir$
>
> Any help appreciated, Bill.
>
>
> On Sat, Jun 13, 2020 at 6:42 AM Brad Gilbert <b2gi...@gmail.com> wrote:
> >
> > Inside of a regex `{…}` will just run some regular Raku code.
> > Code inside of it will most likely have no effect on what the regex
> matches.
> >
> > What you should have written was:
> >
> >     $<sol> = "@W[3]"
> >
> > The thing you were thinking of was:
> >
> >     $<sol> = <{ @W[3] }>
> >
> > Which could have been written as:
> >
> >     <sol={ @W[3] }>
> >
> > ---
> >
> > To have the result of regular Raku code have an effect on the match, it
> has to have <…> around it
> >
> >     'TrueFalse' ~~ / <{ Bool.pick }> /
> >
> > I think it is better if you use "…" if you are just interpolating a
> variable, because that is something you might do outside of a regex as well.
> >
> > ---
> >
> > The reason your code matched is that an empty regex always matches.
> >
> >     'abc' ~~ / "" /
> >     'abc' ~~ / {} /
> >     'abc' ~~ / {'def'} / # still an empty regex as far as the regex
> engine is concerned
> >
> > On Sat, Jun 13, 2020 at 5:35 AM Richard Hainsworth <
> rnhainswo...@gmail.com> wrote:
> >>
> >> I was playing with a regex and array interpolation.
> >>
> >> From the documentation I thought the following comparisons would be the
> same, but they are not.
> >>
> >> What am I missing?
> >>
> >> my @W = <perl weekly challenge with some extra things>;
> >> my $S = 'perlchallengeextrathingswithweeklysome' ; # randomly
> concatenate the words without spaces
> >> say 'yes' if $S ~~ / $<sol>=( { @W[3] } ) /;
> >> say $<sol>;
> >> my $sol = @W[3]; # with
> >> say 'yes' if $S ~~ / $<sol>=( $sol ) /;
> >> say $<sol>;
> >>
> >> <response>
> >> yes
> >> 「」
> >> yes
> >> 「with」
> >>
> >>
> >>
>

Reply via email to