Re: [Rd] capture "->"
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 On Fri, Mar 1, 2024 at 4:18 AM Dmitri Popavenko wrote: > > Hi everyone, > > I am aware this is a parser issue, but is there any possibility to capture > the use of the inverse assignment operator into a formula? > > Something like: > > > foo <- function(x) substitute(x) > > gives: > > > foo(A -> B) > B <- A > > I wonder if there is any possibility whatsoever to signal the use of -> > instead of <- > > Thank you, > Dmitri > > [[alternative HTML version deleted]] > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capture "->"
Duncan, Adrian and all, I decided to look a bit at how various operators are seen by typeof() in light of this discussion and it seems there are quite a few categories as shown below and I suspect more exist. R, like many languages was not initially designed to have some object-oriented aspects and some have been cobbled on over the years including multiple emulations of what it means to be an object. So, explicit or implicit functions may be of different kinds. > typeof(`<-`) [1] "special" > typeof(`->`) Error: object '->' not found > typeof(`+`) [1] "builtin" > typeof(`=`) [1] "special" > typeof(`|>`) Error: object '|>' not found > typeof(`%>%`) [1] "closure" In principle, and someone can probably point to languages that do something like this, all assignments could be centralized into some kind of primitive function call along the lines of: assign("A", 5) Then you could build on that so that the definition of = and <- and even <-- would mean that the following might result in something like the above: A = 5 A <- 5 Bothe become an assign statement like the above meaning a function with a side effect. A <- 5 Would simply be an assign with an added argument specifying the global environment. The other two forms would just invert it so that 5 -> A 5 --> A Simply generate the same assignment statement. I am not sure if such a system would work well for all needs but I can see a language doing that even if it makes it slower. And, in such a system, you could create your own assignment operators or redirect the ones already existing as I considered a possibility. But for some purposes, it may be way easier to have the ability to have some elements of absolutely fixed syntax that allow functionality even before an expression is fully evaluated. As one example, a comma that is not hidden in quotes is generally considered to be a way to determine how to break an expression into parts. Otherwise, how would you know that f(a,b,c) has three arguments. So what happens if your country uses a comma like other use a period as in numbers? Yes, in some environments you can specify that this is being done but it is really not trivial to make a system that is completely flexible in all aspects and allows you to flip from one way of using numbers to the other while also changing any other places the symbols are used in different ways. So the problem becomes whether you can graft the mini language required for dealing with graphs that can include A -> B and A <- B and perhaps A <-> B in ways that are either not seen as they are parsed or even evaluated or that over-ride normal behavior. One obvious idea is to avoid re-using the same symbols. R frequently just wraps symbols in percent signs so you might consider transforming anything to be A %->% B or maybe something like A %_>% or other notations that the parser does not see. It may be a pain but %FORWARD% versus %BACKWARD% or other gimmicks might work enough and you can always substitute out to the original version after you pass this danger zone. And if your formulas are wrapped into some kind of object, they can store several versions or have the ability to do conversions. But those ideas belong in a more mainstream R mailing list. -Original Message- From: Duncan Murdoch Sent: Saturday, March 2, 2024 6:32 AM To: Adrian Dușa ; avi.e.gr...@gmail.com Cc: r-devel Subject: Re: [Rd] capture "->" You can't change the parser. Changes like `+` <- `-` change the function that is called when the expression contains a function call to `+`; this happens in `eval()`, not in `parse()`. There are never any function calls to `->`, because the parser outputs a call to `<-` with the operands reversed when it sees that token. Duncan Murdoch On 02/03/2024 6:06 a.m., Adrian Dușa wrote: > That would have been an elegant solution, but it doesn't seem to work: > >> `->` <- `+` >> 1 -> 3 # expecting 4 > Error in 3 <- 1 : invalid (do_set) left-hand side to assignment > > It is possible to reassign other multiple character operators: >> `%%` <- `+` >> 1 %% 3 > [1] 4 > > The assignment operator `->` is so special for the R parser, that it seems > impossible to change. > > On Fri, Mar 1, 2024 at 11:30 PM wrote: > >> Adrian, >> >> That is indeed a specialized need albeit not necessarily one that cannot >> be done by requiring an alternate way of typing a formula that avoids being >> something the parser sees as needed to do at that level. >> >> In this case, my other questions become moot as I assume the global >> assignment operator and somethings like assign(“xyz”, 5) will not be in the >> way. >> >> What I was wondering about is what happens if you temporarily disable the >> meaning of the assignment operator <- and turn it back on after. >> >> In the following code, for no reason, I redefine + to mean – and then undo >> it: >> >> >>> temp <- `+` >>> `+` <- `-` >>> 5 + 3 >> [1] 2 >>> `+` <- temp >>> 5 + 3 >> [1] 8 >> >
[Rd] Big speedup in install.packages() by re-using connections
Currently download.file() creates and terminates a new TLS connection for each download. This creates a lot of overhead which is expensive for both client and server (in particular the TLS handshake). Modern internet clients (including browsers) re-use connections for many http requests. We can do this in R by creating a persistent libcurl "multi-handle". The R libcurl implementation already uses a multi-handle, however it destroys it after each download, which defeats the purpose. The purpose of the multi-handle is to keep it alive and let libcurl maintain a persistent connection pool. This is particularly relevant for install.packages() which needs to download many files from one and the same server. Here is a bare minimal proof of concept patch that re-uses one and the same multi-handle for all requests in R: https://github.com/r-devel/r-svn/pull/155/files Some quick benchmarking shows that this can lead to big speedups for download.packages() on high-bandwidth servers (such as CI). This quick test to download 100 packages from CRAN showed more than 10x speedup for me: https://github.com/r-devel/r-svn/pull/155 Moreover, I think this may make install.packages() more robust. In CI build logs that download many packages, I often see one or two downloads randomly failing with a TLS-connect error. I am hopeful this problem will disappear when using a single connection to the CRAN server to download all the packages. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capture "->"
On Sat, Mar 2, 2024 at 1:31 PM Duncan Murdoch wrote: > You can't change the parser. Changes like `+` <- `-` change the > function that is called when the expression contains a function call to > `+`; this happens in `eval()`, not in `parse()`. There are never any > function calls to `->`, because the parser outputs a call to `<-` with > the operands reversed when it sees that token. > Another idea would have been to use "=>", which is disabled by default: > substitute(A => B) Error: '=>' is disabled; set '_R_USE_PIPEBIND_' envvar to a true value ... > Sys.setenv(`_R_USE_PIPEBIND_` = TRUE) > substitute(A => B) Error in substitute(`=>`(A, B)) : invalid use of pipe bind symbol (:1:0) The quest is to obtain a valid expression to get past substitute(), but it proves to be more difficult than I initially thought. Dmitri [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capture "->"
You can't change the parser. Changes like `+` <- `-` change the function that is called when the expression contains a function call to `+`; this happens in `eval()`, not in `parse()`. There are never any function calls to `->`, because the parser outputs a call to `<-` with the operands reversed when it sees that token. Duncan Murdoch On 02/03/2024 6:06 a.m., Adrian Dușa wrote: That would have been an elegant solution, but it doesn't seem to work: `->` <- `+` 1 -> 3 # expecting 4 Error in 3 <- 1 : invalid (do_set) left-hand side to assignment It is possible to reassign other multiple character operators: `%%` <- `+` 1 %% 3 [1] 4 The assignment operator `->` is so special for the R parser, that it seems impossible to change. On Fri, Mar 1, 2024 at 11:30 PM wrote: Adrian, That is indeed a specialized need albeit not necessarily one that cannot be done by requiring an alternate way of typing a formula that avoids being something the parser sees as needed to do at that level. In this case, my other questions become moot as I assume the global assignment operator and somethings like assign(“xyz”, 5) will not be in the way. What I was wondering about is what happens if you temporarily disable the meaning of the assignment operator <- and turn it back on after. In the following code, for no reason, I redefine + to mean – and then undo it: temp <- `+` `+` <- `-` 5 + 3 [1] 2 `+` <- temp 5 + 3 [1] 8 [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] capture "->"
That would have been an elegant solution, but it doesn't seem to work: > `->` <- `+` > 1 -> 3 # expecting 4 Error in 3 <- 1 : invalid (do_set) left-hand side to assignment It is possible to reassign other multiple character operators: > `%%` <- `+` > 1 %% 3 [1] 4 The assignment operator `->` is so special for the R parser, that it seems impossible to change. On Fri, Mar 1, 2024 at 11:30 PM wrote: > Adrian, > > That is indeed a specialized need albeit not necessarily one that cannot > be done by requiring an alternate way of typing a formula that avoids being > something the parser sees as needed to do at that level. > > In this case, my other questions become moot as I assume the global > assignment operator and somethings like assign(“xyz”, 5) will not be in the > way. > > What I was wondering about is what happens if you temporarily disable the > meaning of the assignment operator <- and turn it back on after. > > In the following code, for no reason, I redefine + to mean – and then undo > it: > > > > temp <- `+` > > `+` <- `-` > > 5 + 3 > [1] 2 > > `+` <- temp > > 5 + 3 > [1] 8 > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel