User defined infix operators and whitespace

--94eb2c052f761362a70556645342
Content-Type: text/plain; charset="UTF-8"

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

--94eb2c052f761362a70556645342
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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

--94eb2c052f761362a70556645342--
0
simon
8/10/2017 11:16:03 AM
perl.perl6.users 881 articles. 0 followers. Follow

2 Replies
33 Views

Similar Articles

[PageSpeed] 32

--001a114091fc9481130556647d76
Content-Type: text/plain; charset="UTF-8"

I think there are a couple answers. The simple one is yes -- embrace the
whitespace. Maybe wrap them in ()', like (2 d 6).

Another line of ideas is wrapping things with some other operators. Maybe a
special quoting operator or a converter.

  [[2d6]] # double for dice!
  "2d6":dice # postfix. This used to work but maybe doesn't now
  dice[ 2d6 ] # circumfix

And then the third thing that comes to mind is adding a slang, though that
is, I believe, the least stable language-level approach. Dunno.

--Brock


On Thu, Aug 10, 2017 at 7:16 AM, 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
>

--001a114091fc9481130556647d76
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I think there are a couple answers. The simple one is yes =
-- embrace the whitespace. Maybe wrap them in ()&#39;, like (2 d 6).<div><b=
r></div><div>Another line of ideas is wrapping things with some other opera=
tors. Maybe a special quoting operator or a converter.</div><div><br></div>=
<div>=C2=A0 [[2d6]] # double for dice!</div><div>=C2=A0 &quot;2d6&quot;:dic=
e # postfix. This used to work but maybe doesn&#39;t now</div><div>=C2=A0 d=
ice[ 2d6 ] # circumfix</div><div><br></div><div>And then the third thing th=
at comes to mind is adding a slang, though that is, I believe, the least st=
able language-level approach. Dunno.</div><div><br></div><div>--Brock</div>=
<div><br></div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quo=
te">On Thu, Aug 10, 2017 at 7:16 AM, Simon Proctor <span dir=3D"ltr">&lt;<a=
 href=3D"mailto:simon.proctor@gmail.com" target=3D"_blank">simon.proctor@gm=
ail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D=
"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D=
"ltr"><div><div><div><div><div>So I had a crazy little idea. I&#39;ve playe=
d the odd bit of roleplaying in my time and wanted to created a &#39;d&#39;=
 operator.<br><br></div>Quite simple really. <br><br>sub infix:&lt;d&gt; ( =
Int $num, Int $size ) { [+] (1..$size).roll($num) };<br><br>sub prefix:&lt;=
d&gt; ( Int $size ) { 1 d $size }<br><br></div>Gives us 3 d 6 to roll 3 six=
 sided dice or a prefix d 10 for a single 10 sided dice.<br><br></div>Excep=
t... I&#39;d really like to write 3d6 or d10 but the parser barfs.<br><br><=
/div>Am I going to just have to live with that? Or did I miss something obv=
ious? <br><br></div><div>Obviously it&#39;s possible to have operators that=
 ignore whitespace (1+1 works just fine) but is it possibly for user define=
d ones?<br><br></div><div>Possibly more serious ones.<span class=3D"HOEnZb"=
><font color=3D"#888888"><br><br></font></span></div><span class=3D"HOEnZb"=
><font color=3D"#888888"><div>Simon<br></div></font></span></div>
</blockquote></div><br></div>

--001a114091fc9481130556647d76--
0
awwaiid
8/10/2017 11:28:06 AM
--94eb2c077ab4d4988b0557a3af78
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

It seems to work without spaces if you choose a symbol that is not
letter-like.

# =F0=9F=8E=B2U+1F3B2 Game Die
sub infix:<=F0=9F=8E=B2> ( Int $num, Int $size ) { [+] (1..$size).roll($num=
) };
sub prefix:<=F0=9F=8E=B2> ( Int $size ) { 1=F0=9F=8E=B2$size }
say =F0=9F=8E=B210;
say 4=F0=9F=8E=B26;

# =E2=9B=8F U+26CF Pick
sub infix:<=E2=9B=8F> ( Int $num, Int $size ) { [+] (1..$size).roll($num) }=
;
sub prefix:<=E2=9B=8F> ( Int $size ) { 1=E2=9B=8F$size }
say =E2=9B=8F10;
say 4=E2=9B=8F6;

# Not working
# =E2=85=86 U+2146 Double-Struck Italic Small D
#sub infix:<=E2=85=86> ( Int $num, Int $size ) { [+] (1..$size).roll($num) =
};
#sub prefix:<=E2=85=86> ( Int $size ) { 1=E2=85=86$size }
#say =E2=85=8610;
#say 4=E2=85=866;


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 m=
y
> 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
>



--=20
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

--94eb2c077ab4d4988b0557a3af78
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">It seems to work without spaces if you choose a symbol tha=
t is not letter-like.<div><br></div><div><div># =F0=9F=8E=B2U+1F3B2 Game Di=
e</div><div>sub infix:&lt;=F0=9F=8E=B2&gt; ( Int $num, Int $size ) { [+] (1=
...$size).roll($num) };</div><div>sub prefix:&lt;=F0=9F=8E=B2&gt; ( Int $siz=
e ) { 1=F0=9F=8E=B2$size }</div><div>say =F0=9F=8E=B210;</div><div>say 4=F0=
=9F=8E=B26;</div><div><br></div><div># =E2=9B=8F U+26CF Pick</div><div>sub =
infix:&lt;=E2=9B=8F&gt; ( Int $num, Int $size ) { [+] (1..$size).roll($num)=
 };</div><div>sub prefix:&lt;=E2=9B=8F&gt; ( Int $size ) { 1=E2=9B=8F$size =
}</div><div>say =E2=9B=8F10;</div><div>say 4=E2=9B=8F6;</div><div><br></div=
><div># Not working</div><div># =E2=85=86 U+2146 Double-Struck Italic Small=
 D</div><div>#sub infix:&lt;=E2=85=86&gt; ( Int $num, Int $size ) { [+] (1.=
..$size).roll($num) };</div><div>#sub prefix:&lt;=E2=85=86&gt; ( Int $size )=
 { 1=E2=85=86$size }</div><div>#say =E2=85=8610;</div><div>#say 4=E2=85=866=
;</div></div><div><br></div></div><div class=3D"gmail_extra"><br><div class=
=3D"gmail_quote">On 10 August 2017 at 21:16, Simon Proctor <span dir=3D"ltr=
">&lt;<a href=3D"mailto:simon.proctor@gmail.com" target=3D"_blank">simon.pr=
octor@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" =
style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><di=
v dir=3D"ltr"><div><div><div><div><div>So I had a crazy little idea. I&#39;=
ve played the odd bit of roleplaying in my time and wanted to created a &#3=
9;d&#39; operator.<br><br></div>Quite simple really. <br><br>sub infix:&lt;=
d&gt; ( Int $num, Int $size ) { [+] (1..$size).roll($num) };<br><br>sub pre=
fix:&lt;d&gt; ( Int $size ) { 1 d $size }<br><br></div>Gives us 3 d 6 to ro=
ll 3 six sided dice or a prefix d 10 for a single 10 sided dice.<br><br></d=
iv>Except... I&#39;d really like to write 3d6 or d10 but the parser barfs.<=
br><br></div>Am I going to just have to live with that? Or did I miss somet=
hing obvious? <br><br></div><div>Obviously it&#39;s possible to have operat=
ors that ignore whitespace (1+1 works just fine) but is it possibly for use=
r defined ones?<br><br></div><div>Possibly more serious ones.<span class=3D=
"HOEnZb"><font color=3D"#888888"><br><br></font></span></div><span class=3D=
"HOEnZb"><font color=3D"#888888"><div>Simon<br></div></font></span></div>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div class=
=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><d=
iv>Norman Gaywood, Computer Systems Officer</div><div>School of Science and=
 Technology<br>University of New England</div><div>Armidale=C2=A0NSW 2351, =
Australia<br><br><a href=3D"mailto:ngaywood@une.edu.au" target=3D"_blank">n=
gaywood@une.edu.au</a>=C2=A0=C2=A0<a href=3D"http://turing.une.edu.au/~ngay=
wood" target=3D"_blank">http://turing.une.edu.au/~ngaywood</a></div><div>Ph=
one: +61 (0)2 6773 2412 =C2=A0Mobile: +61 (0)4 7862 0062<br><br>Please avoi=
d sending me Word or Power Point attachments.<br>See <a href=3D"http://www.=
gnu.org/philosophy/no-word-attachments.html" target=3D"_blank">http://www.g=
nu.org/philosophy/no-word-attachments.html</a><br></div></div></div>
</div>

--94eb2c077ab4d4988b0557a3af78--
0
ngaywood
8/26/2017 8:16:44 AM
Reply: