[Rd] [External] Re: capture "->"

Barry Rowlingson b@row||ng@on @end|ng |rom |@nc@@ter@@c@uk
Mon Mar 4 10:40:39 CET 2024


It seems like you want to use -> and <- as arrows with different meanings
to "A gets the value of B" in your package, as a means of writing
expressions in your package language.

Another possibility would be to use different symbols instead of the
problematic -> and <-, for example you could use <.~ and ~.> which are not
at all flipped or changed before you get a chance to parse your expression.
It might make your language parser a bit trickier though. Let's see how
these things turn into R's AST using `lobstr`:

 > library(lobstr)
 > ast(A ~.> B)
█─`~`
├─A
└─█─`>`
  ├─.
  └─B
 > ast(A <.~ B)
█─`~`
├─█─`<`
│ ├─A
│ └─.
└─B

You'd have to unpick that tree to figure out you've got A and B on either
side of your expression, and that the direction of the expression is L-R or
R-L.

You could also use -.> and <.- symbols, leading to a different tree

 > ast(A -.> B)
█─`>`
├─█─`-`
│ ├─A
│ └─.
└─B
 > ast(A <.- B)
█─`<`
├─A
└─█─`-`
  ├─.
  └─B

Without knowing the complexity of your language expressions (especially if
it allows dots and minus signs with special meanings) I'm not sure if A)
this will work or B) this will bend your brain in horrible directions in
order to make it work... Although you don't need to parse the AST as above,
you can always deparse to get the text version of it:

 > textex = function(x){deparse(substitute(x))}
 > textex(A <.~ B)
[1] "A < . ~ B"

The <.~ form has an advantage over the <.- form if you want to do complex
expressions with more than one arrow, since the ~ form is syntactically
correct but the - form isnt:

 > textex(A <.~ B ~.> C)
[1] "A < . ~ B ~ . > C"
 > textex(A <.- B -.> C)
Error: unexpected '>' in "textex(A <.- B -.>"


Barry


On Sun, Mar 3, 2024 at 12:25 PM Dmitri Popavenko <dmitri.popavenko using gmail.com>
wrote:

> This email originated outside the University. Check before clicking links
> or attachments.
>
> On Sat, Mar 2, 2024 at 7:58 PM Gabor Grothendieck <ggrothendieck using gmail.com
> >
> wrote:
>
> > Would it be good enough to pass it as a formula?  Using your definition
> of
> > foo
> >
> >   foo(~ A -> result)
> >   ## result <- ~A
> >
> >   foo(~ result <- A)
> >   ## ~result <- A
> >
>
> Yes, to pass as a formula would be the idea.
> It's just that the parser inverses "~A -> result" into "result <- ~A".
> We are seeking for any way possible to flag this inversion.
>
> Avi, thank you for your efforts too. Wrapping symbols into percent signs is
> an option, but as Duncan says it is much more intuitive to just quote the
> expression.
> The challenge is to somehow flag the parser inversion, otherwise a quoted
> expression seems to be the only solution possible.
>
> Regards,
> Dmitri
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list