Front page | perl.perl6.language |
Postings from July 2010
Re: Array membership test?
Thread Previous
|
Thread Next
From:
Aaron Sherman
Date:
July 30, 2010 14:22
Subject:
Re: Array membership test?
Message ID:
AANLkTin_3WyHwai-TE_0usV71YdU+=-rfVg941Lvac1r@mail.gmail.com
I may be misunderstanding something. I haven't really looked into list
searching much, but there seem to be some very odd and unexpected
results, here.
On Thu, Jul 29, 2010 at 7:52 PM, Jonathan Worthington
<jonathan@jnthn.net> wrote:
>> my @x = 1,2,3; say ?@x.grep(2); say ?@x.grep(4);
> 1
> 0
>
> Though more efficient would be:
>
>> my @x = 1,2,3; say ?@x.first(2); say ?@x.first(4);
> 1
> 0
The above only works if the value that you are searching for does
something sane when it's the RHS of a smart-match. Try searching for
False this way and you'll be sad, just for example (PS: I think the
behavior of False ~~ False is unsmart).
Actually, it looks like === is the right way to do this for value
types like 2 and True, but right now Rakudo doesn't do the right
thing:
my @x = 1,2,3,False; say ?@x.first: * === 2; say ?@x.first: * ===
False; say ?@x.first: * === True;
1
0
1
I think it's just smart-matching, which is definitely not correct
(False === False does do the right thing when you executed it on its
own, though).
If you really want odd, try:
say [1,2,3].first: * === True;
Result: 1
and
say [5,2,3].first: * === True;
Result: Rakudo exits silently with no newline
So, the right way to search for value types in a list... is highly
questionable right now. ;-)
--
Aaron Sherman
Email or GTalk: ajs@ajs.com
http://www.ajs.com/~ajs
Thread Previous
|
Thread Next