From w||||@mwdun|@p @end|ng |rom gm@||@com  Mon Mar  4 17:51:00 2024
From: w||||@mwdun|@p @end|ng |rom gm@||@com (Bill Dunlap)
Date: Mon, 4 Mar 2024 08:51:00 -0800
Subject: [Rd] [External] Re: capture "->"
In-Reply-To: <CAJL_pogKj-WGoQ1LiarBH--ebtrmAo5cQQ=aJt3suRU3+2ZHSw@mail.gmail.com>
References: <CAJL_pojF-Qc8YA0_eB+Ab9w3qD5sgMkjm0C3b0ooOmoOUy769w@mail.gmail.com>
 <CAP01uRk-aKErTBoWNUeFaF23doSJP5v8nz4WGT+jxCgiFXip7A@mail.gmail.com>
 <a75b4ccc217048fc860fd70db8766668@LO6P265MB6080.GBRP265.PROD.OUTLOOK.COM>
 <CANVKczOaiVjS4nNm2ht1tHhz0SHjeKSHkneeAHbW1pyShkgsOw@mail.gmail.com>
 <CAJL_pogKj-WGoQ1LiarBH--ebtrmAo5cQQ=aJt3suRU3+2ZHSw@mail.gmail.com>
Message-ID: <CAHqSRuQ39Kt+Xgq6H-9m-q9tS7DgVe6VCUaiKWx6A8CzYd8JQw@mail.gmail.com>

Maybe someone has already suggested this, but if your functions accepted
strings you could use sub or gsub to replace the -> with a symbol that
parsed at the same precedence as <-,
say <<-.  Then parse it and deal with it.  When it is time to display the
parsed and perhaps manipulated formulae to the user, deparse it and do the
reverse replacement.

> encode <- function(string)gsub(perl=TRUE, "->", "<<-", x=string)
> decode <- function(string)gsub(perl=TRUE, "<<-", "->", x=string)
> rightArrow <- as.name("<<-")
> leftArrow <- as.name("<-")
> ast1 <- parse(text=encode("x1 + x2 -> a3"))[[1]]
> ast2 <- parse(text=encode("y4 <- b5 + (b6 / b7)"))[[1]]
> identical(ast1[[1]], rightArrow)
[1] TRUE
> identical(ast2[[1]], leftArrow)
[1] TRUE
> ast1[[3]] <- as.name("new_a3")
> decode(deparse(ast1))
[1] "x1 + x2 -> new_a3"

-Bill

On Mon, Mar 4, 2024 at 1:59?AM Dmitri Popavenko <dmitri.popavenko at gmail.com>
wrote:

> Dear Barry,
>
> In general, I believe users are already accustomed with the classical
> arrows "->" and "<-" which are used as such in quoted expressions.
> But I agree that "-.>" is a very neat trick, thanks a lot. A small dot,
> what a difference.
>
> All the best,
> Dmitri
>
> On Mon, Mar 4, 2024 at 11:40?AM Barry Rowlingson <
> b.rowlingson at lancaster.ac.uk> wrote:
>
> > 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 at 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 at 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 at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



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