develooper Front page | perl.perl6.users | Postings from August 2017

Re: User defined infix operators and whitespace

From:
Norman Gaywood
Date:
August 26, 2017 08:17
Subject:
Re: User defined infix operators and whitespace
Message ID:
CAHJ-_U4t430ybqcH5-Tv+qH9TcqNiMRH_gVa2p2Q-FOq0PN3jg@mail.gmail.com
It seems to work without spaces if you choose a symbol that is not
letter-like.

# đŸŽČU+1F3B2 Game Die
sub infix:<đŸŽČ> ( Int $num, Int $size ) { [+] (1..$size).roll($num) };
sub prefix:<đŸŽČ> ( Int $size ) { 1đŸŽČ$size }
say đŸŽČ10;
say 4đŸŽČ6;

# ⛏ U+26CF Pick
sub infix:<⛏> ( Int $num, Int $size ) { [+] (1..$size).roll($num) };
sub prefix:<⛏> ( Int $size ) { 1⛏$size }
say ⛏10;
say 4⛏6;

# Not working
# ⅆ U+2146 Double-Struck Italic Small D
#sub infix:<ⅆ> ( Int $num, Int $size ) { [+] (1..$size).roll($num) };
#sub prefix:<ⅆ> ( Int $size ) { 1ⅆ$size }
#say ⅆ10;
#say 4ⅆ6;


On 10 August 2017 at 21:16, Simon Proctor <simon.proctor@gmail.com> wrote:

> So I had a crazy little idea. I've played the odd bit of roleplaying in my
> time and wanted to created a 'd' operator.
>
> Quite simple really.
>
> sub infix:<d> ( Int $num, Int $size ) { [+] (1..$size).roll($num) };
>
> sub prefix:<d> ( Int $size ) { 1 d $size }
>
> Gives us 3 d 6 to roll 3 six sided dice or a prefix d 10 for a single 10
> sided dice.
>
> Except... I'd really like to write 3d6 or d10 but the parser barfs.
>
> Am I going to just have to live with that? Or did I miss something
> obvious?
>
> Obviously it's possible to have operators that ignore whitespace (1+1
> works just fine) but is it possibly for user defined ones?
>
> Possibly more serious ones.
>
> Simon
>



-- 
Norman Gaywood, Computer Systems Officer
School of Science and Technology
University of New England
Armidale NSW 2351, Australia

ngaywood@une.edu.au  http://turing.une.edu.au/~ngaywood
Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062

Please avoid sending me Word or Power Point attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html



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