develooper Front page | perl.perl5.porters | Postings from December 2017

Re: Revisiting smart match

Thread Previous | Thread Next
From:
Dan Book
Date:
December 7, 2017 20:52
Subject:
Re: Revisiting smart match
Message ID:
CABMkAVUp4mWR_WfrTK_0=gM-Kyq=ooDriuQOf71WWQn=pd2x=w@mail.gmail.com
On Thu, Dec 7, 2017 at 3:44 PM, Zefram <zefram@fysh.org> wrote:
>
>
> For your match-any use case, you need some supporting code amounting
> to this:
>
>     package MatchAny {
>         use overload "~~" => sub {
>             foreach(@{$_[0]}) {
>                 return 1 if $_[1] ~~ $_;
>             }
>             return 0;
>         };
>     }
>     sub any { bless(\@_, "MatchAny") }
>
> Then you write "$a ~~ any($b, $c, $d)" in place of "$a ~~ [$b, $c, $d]".


To add, such matchers are already available from CPAN in
https://metacpan.org/pod/Smart::Match which should work for both new and
old smartmatch. It also gives you numwise() and stringwise() matchers so
you can explicitly say for example: $x ~~ any(numwise(@numbers)).

-Dan

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About