Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Any.match do?

It has the deceivingly simple code:

 method match(Any:U: |) { self.Str; nqp::getlexcaller('$/') = Nil }

However, this is the behavior it has:

(^3).match(1) # OUTPUT: «「1」␤»

So far, so good.

say (1,3 ... * ).match(77); # OUTPUT: «Nil␤»

Ooookey. What's happenning now?

say (1,3 ... * ).match(1);    # OUTPUT: «Nil␤»
say (1,3 ... * ).match(/\d/); # OUTPUT: «Nil␤»

Does not like sequences.

say (^10).match(/\d/); # OUTPUT: «「0」␤»

OK, makes sense again.

say <a b c>.match(/\w/); # OUTPUT: «「a」␤»

Back to normal. So is it that it does not like Seqs? I assume, because I've looked at the other classes' code and match is not reimplemented, all of them are calling that code. But I fail to see how returning a string and setting a variable from NPQ does that, or why it does not work on sequences.

like image 467
jjmerelo Avatar asked Feb 22 '19 06:02

jjmerelo


People also ask

What is anyMatch?

The anyMatch() method returns true if at least one element satisfies the condition provided by predicate , else false .

Is anyMatch a terminal operation?

The Java Stream anyMatch() method is a terminal operation that takes a single Predicate as parameter, starts the internal iteration of the Stream , and applies the Predicate parameter to each element.

What is anyMatch in Java?

The anyMatch() method in the IntStream class in Java returns whether any elements of this stream match the provided predicate. The syntax is as follows boolean anyMatch(IntPredicate predicate) To work with the IntStream class in Java, import the following package. import java.

What is difference between anyMatch and allMatch?

anyMatch() returns true if any of the elements in a stream matches the given predicate. If the stream is empty or if there's no matching element, it returns false . allMatch() returns true only if ALL elements in the stream match the given predicate.


1 Answers

.match is a search for a needle in a single haystack string. An infinite sequence stringifies to '...'.

say (1,3 ... 9).Str;        # 1 3 5 7 9
say (1,3 ... 9).match: '1'; # 「1」

say (1,3 ... *).Str;        # ...
say (1,3 ... *).match: '.'; # 「.」

How I worked this out

First, you're looking at the wrong method definition:

method match(Any:U: |) { ... }

Any:U is kinda like Any $ where not .defined except if it matched you would get the error message "Parameter '<anon>' of routine 'match' must be a type object of type 'Any', not an object instance ...".

But you're passing a defined Seq. So your .match calls don't dispatch to the method definition you're looking at.

To find out what a method dispatches to, use:

say (1,3 ... *).^lookup('match').package ; # (Cool)

A defined Seq will thus dispatch to the Cool code:

method match(Cool:D: |c) {
    ...
    self.Stringy.match(|c)
}

So, next:

say (1,3 ... *).^lookup('Stringy').package ; # (Mu)

And the code:

multi method Stringy(Mu:D $:) { self.Str }

So check:

say (1,3 ... *).Str; # ...

Bingo.

And confirm:

say (1,3 ... *).match: '.'; # 「.」
like image 57
raiph Avatar answered Sep 28 '22 05:09

raiph