[R] 'closure' (was "stats:: spline's method could not be monoH.FC")

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Mon May 4 16:04:18 CEST 2020


Just about this one some important term  'closure', hence I'm
modifying the subject. 

Note we came here from my 2nd reason why I had added 'monoH.FC'
feature only for splinefun() and not for spline() :

- splinefun() is using's R feature of "(non-trivial) closure", i.e, 
  it returns a *function* containing its own state

-------------

Yes, there is some vagueness / ambiguity about how the term
"closure" is used within R documentation and teaching :

Yes, indeed, the term  stems from lisp ("Scheme" more
specifically according to
 https://en.wikipedia.org/wiki/Closure_(computer_programming) ,
but you should remember that R originally had been implemented
as a "lisp with S-syntax" (that's my paraphrasing), see
 https://en.wikipedia.org/wiki/R_(programming_language)
   and (in the box on the right hand side) its list of 

    "Influenced by" : Common Lisp, S, Scheme[2], XLispStat

and [2] is Ross Ihaka's famous Interface paper :

    Ihaka, Ross (1998). R : Past and Future History (PDF) (Technical report).
    Statistics Department, The University of Auckland, Auckland, New Zealand.
    https://www.stat.auckland.ac.nz/~ihaka/downloads/Interface98.pdf

And that's the reason why  typeof(f)   for all functions 'f'
which are not primitive is "closure".
All such functions (i.e. *not* the primitives) have an
environment, as Mark Leeds explains,  and what this means and
why this is very important is beyond 'R-help'.
Inside R's own C code, all such R functions are "closures", and
programmers (incl R corers) who think more about the low level
view of R objects would use the term like that ex

OTOH, Hadley Wickham has written a book "Advanced R"  which has been
the best book about advanced R by far (in my view, notably
before it morphed (towards the 2nd edition) to use more and more
non-base R packages).  There, he used "Closure" in a different,
stricter sense, starting the section  'Closures' with

    “An object is data with functions.
     A closure is a function with data.” — John D. Cook

Now, most functions have only a "trivial environment" (my own
  terminology,  when I'm teaching "advanced R" courses/classes,
  see https://github.com/mmaechler/ProgRRR/ for some teaching material)

"trivial environment" meaning that their environment is
- either the namespace belonging to the package the function is part of
- or .GlobalEnv  { which is the same as globalenv() }

and most functions with non-trivial environment are just
"helper" functions defined inside other functions which are very
short lived (during the evaluation of the outer function's call).

Now the remaining few functions with non-trivial environments
that you see in "base R"  are those returned by

  splinefun(), approxfun(),  ecdf(), or stepfun()

where the last two actually are implemented via approxfun().

-- -- --

I hope this has been useful  "writeup" about
'closure' ..

Best,
Martin

Martin Maechler
ETH Zurich  and  R Core team



More information about the R-help mailing list