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 832 articles. 0 followers. Follow

1 Replies
15 Views

Similar Articles

[PageSpeed] 56

--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
Reply: