Abby: Here is an article on environments/closures which might be useful to you. I was reviewing environments recently and it was a clear explanation of how environments/closures work in R. Even though it's from 2000, I'm pretty certain that everything said in it still holds. On Sun, May 3, 2020 at 12:16 AM Abby Spurdle <spurdle.a at gmail.com> wrote:> > and just today a colleague asked me about spline interpolation > > with general 2nd derivative boundary conditions > > s''(x_1) = s2_1, s''(x_n) = s2_2 > > It should possible via cubic Hermite splines. > A nontrivial design decision in my package was the computation of > slopes at the endpoints. > (Something which I got wrong, twice...) > > My guess is that I could write a function in about 60 to 90 minutes, > including all the testing and calculus. > > However, I need to note two things: > (1) Cubic Hermite splines do not have a continuous second derivative. > (2) Specifying the second derivatives (at the endpoints) would prevent > the user from specifying the first derivatives (aka the slopes). > > Could you please confirm if the function would still be of interest...? > > And completely diverging... > > it returns a *function* containing its own state > > I use function objects extensively. > However, I haven't been able to find a definitive guide to terminology. > The word "closure" appears to have a lisp origin, but it usage in R is > a bit grey. > > Many of my functions have attributes. > However, I recognize that the use the function environments is more > popular, and has the advantage that the user can take advantage of > lexical scoping, but has the disadvantage that copying function > objects can have unexpected results. > > Recently, I've been using the terms "Self-Referencing Function > Objects" and "Functions Bundled with Data", but was wondering if these > terms are sub-optimal...? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-------------- next part -------------- A non-text attachment was scrubbed... Name: ih_gent_lexical.pdf Type: application/pdf Size: 220482 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200503/fe569c8d/attachment.pdf>
Hi Mark, The article is good. However, there's still some grey areas. The documentation for base::typeof equates a closure with a function. However, the article defines a closure as a function together with an environment. A very minor difference I know, but it creates the problem that the word closure is used inconsistently. And that's without even getting into "frames". Equating a closure with a function (only), is perhaps misleading...??? Also, re-iterating I use function objects with attributes. (I prefer this approach, because I can make copies of function objects, and if necessary modify them). My guess is that doesn't meet the definition of an R closure (if you ignore the environment)...???, and it's unclear whether it meets the definition of a closure, more generally...??? So, I've still got the same problem, of how to refer to functions that have *either* attributes *or* environments, containing data. Maybe I should just stick to "Self-Referencing Function Objects" and "Functions Bundled with Data"...??? One last thing, the last time I read S4 documentation, I couldn't tell if it was possible to have S4-based function objects, and if so, could the body of the S4-based function object (while being called) access it's own slots...??? On Sun, May 3, 2020 at 4:27 PM Mark Leeds <markleeds2 at gmail.com> wrote:> > Abby: Here is an article on environments/closures which might be useful to you. I was reviewing environments recently and it > was a clear explanation of how environments/closures work in R. Even though it's from 2000, I'm pretty certain that everything > said in it still holds.
Hi Abby: your questions are good but I can't help because I don't need to actually use them to that level of detail. I only have to understand it in its generalities. Hopefully, Martin or one of the other R-core people or guRus can answer your questions in a definitive and clear way. But, as far as the term closure, I think that they referring to the environment that a function looks in after its looks in its own environment. So, if you defined a function say, foo, in the global environment, then the foo's closure is the global environment because, when finding the values of variables, foo will first look in its environment and then its closure. So, closure is just an environment that's the parent of a function's environment. It answers the question of where a function looks next when it can't find the values of some of the variables that it needs to evaluate. I'd also love to hear an explanation of "frame' because that term has always confused me I think ( emphasis on think ), when a function, foo, gets called, it creates a new environment to evaluate its arguments and this new environment of foo is called a "frame". But where is it and what is its parent ? Is "frame" just a fancy term for the function's own environment ? or is the function's own environment the parent of its evaluation frame I'm not sure. I have other documents that may help ( I recently did a document gathering but I only read the one I sent you ) but I think it's better to wait to see if anyone can help here first. Then, if not, I can send you some other things. Oh, the slot question was also interesting but I've never used S4. Also, this list has gotten thinner over the years so another place to ask is of course stackoverflow. On Sun, May 3, 2020 at 6:19 AM Abby Spurdle <spurdle.a at gmail.com> wrote:> Hi Mark, > > The article is good. > However, there's still some grey areas. > > The documentation for base::typeof equates a closure with a function. > However, the article defines a closure as a function together with an > environment. > A very minor difference I know, but it creates the problem that the > word closure is used inconsistently. > And that's without even getting into "frames". > > Equating a closure with a function (only), is perhaps misleading...??? > > Also, re-iterating I use function objects with attributes. > (I prefer this approach, because I can make copies of function > objects, and if necessary modify them). > My guess is that doesn't meet the definition of an R closure (if you > ignore the environment)...???, and it's unclear whether it meets the > definition of a closure, more generally...??? > > So, I've still got the same problem, of how to refer to functions that > have *either* attributes *or* environments, containing data. > Maybe I should just stick to "Self-Referencing Function Objects" and > "Functions Bundled with Data"...??? > > One last thing, the last time I read S4 documentation, I couldn't tell > if it was possible to have S4-based function objects, and if so, could > the body of the S4-based function object (while being called) access > it's own slots...??? > > > On Sun, May 3, 2020 at 4:27 PM Mark Leeds <markleeds2 at gmail.com> wrote: > > > > Abby: Here is an article on environments/closures which might be useful > to you. I was reviewing environments recently and it > > was a clear explanation of how environments/closures work in R. Even > though it's from 2000, I'm pretty certain that everything > > said in it still holds. >[[alternative HTML version deleted]]
Abby: Just one other thing. A friend of mine recommended reading the R-language manual which I haven't read in many years. I don't know if it's because I'm more expeRienced or just plain oldeR but I started it today and it's not nearly as daunting as I remember it being in the past. It's going to take me some time but I highly recommend checking it out if you haven't already. Maybe it's like a fine wine and just gets better with age !!!!!!! Good luck. Mark ---------- Forwarded message --------- From: Abby Spurdle <spurdle.a at gmail.com> Date: Sun, May 3, 2020 at 6:19 AM Subject: Re: [R] stats:: spline's method could not be monoH.FC To: Mark Leeds <markleeds2 at gmail.com> Cc: Martin Maechler <maechler at stat.math.ethz.ch>, Samuel Granjeaud IR/Inserm <samuel.granjeaud at inserm.fr>, r-help <r-help at r-project.org> Hi Mark, The article is good. However, there's still some grey areas. The documentation for base::typeof equates a closure with a function. However, the article defines a closure as a function together with an environment. A very minor difference I know, but it creates the problem that the word closure is used inconsistently. And that's without even getting into "frames". Equating a closure with a function (only), is perhaps misleading...??? Also, re-iterating I use function objects with attributes. (I prefer this approach, because I can make copies of function objects, and if necessary modify them). My guess is that doesn't meet the definition of an R closure (if you ignore the environment)...???, and it's unclear whether it meets the definition of a closure, more generally...??? So, I've still got the same problem, of how to refer to functions that have *either* attributes *or* environments, containing data. Maybe I should just stick to "Self-Referencing Function Objects" and "Functions Bundled with Data"...??? One last thing, the last time I read S4 documentation, I couldn't tell if it was possible to have S4-based function objects, and if so, could the body of the S4-based function object (while being called) access it's own slots...??? On Sun, May 3, 2020 at 4:27 PM Mark Leeds <markleeds2 at gmail.com> wrote:> > Abby: Here is an article on environments/closures which might be usefulto you. I was reviewing environments recently and it> was a clear explanation of how environments/closures work in R. Eventhough it's from 2000, I'm pretty certain that everything> said in it still holds.[[alternative HTML version deleted]]