develooper Front page | perl.perl6.language | Postings from March 2012

Re: How to make a new operator.

Thread Previous | Thread Next
From:
Solomon Foster
Date:
March 21, 2012 17:13
Subject:
Re: How to make a new operator.
Message ID:
CALpVjkj8aDedk6dUP4ke7g8-HNbshK5R2nBQEx_vuJD1zMRgcA@mail.gmail.com
On Wed, Mar 21, 2012 at 7:52 PM, Daniel Carrera <dcarrera@gmail.com> wrote:
>> * >= $b --- this determines where the sequence ends:  when the current value
>> is greater or equal to $b.
>
> So...  after the "..." you have an anonymous function that has to
> return 'True' for the sequence to end? Seems strange, but it works:
>
> # A "function" that always returns True => List of one item.
>> 2,5...True
> 2

It actually smartmatches whatever is on the right hand side against
the sequence, and stops when the smartmatch returns True.  It just
"happens" that you smartmatch an anonymous function, it executes the
function and returns its result.

In particular, note that True there is not an anonymous function, it's
just the value True.  You'd need to say { True } to get an anonymous
function which returns True.

Examples:

2, 5 ... 11 # smartmatch is true when you hit 11
2, 5 ... True # stop right away
2, 5 ... False # never stop
2, 5 ... * # shorter way of saying never stop
2, 1, 1/2 ... Num # stop when the number switches from a Rat to a Num
'a' ... /f/ # stop when the regular expression matches

-- 
Solomon Foster: colomon@gmail.com
HarmonyWare, Inc: http://www.harmonyware.com

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