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

Re: How to make a new operator.

Thread Previous | Thread Next
From:
Daniel Carrera
Date:
March 22, 2012 02:21
Subject:
Re: How to make a new operator.
Message ID:
CAEaabNh7ipT_60+rsGMmrxUhmssXriEY-0=Udu7UGNDmwxq5gA@mail.gmail.com
On 22 March 2012 04:59, Jonathan Lang <dataweaver@gmail.com> wrote:
> My understanding is if you want to count by threes, starting at 2 and ending at 14, you should be able to write:
>
>   2, 5 ... 14

That certainly looks very intuitive, and it is similar to what I would
write in an email. The only annoyance is that I have to do a bit of
mental arithmetic. But I guess that's ok. Especially since most of the
time you'd probably want to write 2,5...* anyway.

> So:
>
>    1, 3 ... 13 # same as 1,3,5,7,9,11,13
>    1 ... 10 # same as 1,2,3,4,5,6,7,8,9,10
>    1, 2, 4 ... 100 # same as 1,2,4,8,16,32,64


That last one doesn't work on Rakudo :-(

> Meanwhile, using Whatever for the test condition means "keep the series going indefinitely":
>
>    1, 3 ... * # every positive odd number.
>    1 ... * # all counting numbers.
>    1, 2, 4 ... * # all powers of 2.


Yeah, and those are very convenient.


> And using '...^' instead of '...' changes the default final test condition from '> $n' to '>= $n':
>
>    1, 3 ...^ 13 # same as 1,3,5,7,9,11
>    1 ...^ 10 # same as 1,2,3,4,5,6,7,8,9
>    1, 2, 4 ...^ 100 # same as 1,2,4,8,16,32,64


Ok. I hadn't thought of doing that.


> In short, what Damian is talking about is the more powerful and reliable use of the syntax; but the above approach (assuming it has been properly implemented) is a more intuitive use that covers the most common cases.  Make common things easy, and make uncommon things possible.


Yeah.

>
> Likewise, using Whatever in conjunction with operators is there to provide an intuitive way to calculate the next term from the previous one(s):
>
>    1, *+2 ... 13 # start at 1, step by 2s, stop at 13.
>    1, 1, *+* ... * # each new term is the sum of the previous two.

Oh! That second one is cool. a one line implementation of the
Fibonacci sequence.

Cheers,
Daniel.
-- 
I'm not overweight, I'm undertall.

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