[R] What is a 'closure'?

2012-01-19 Thread Ajay Askoolum
The R Language Definition at 
http://cran.r-project.org/doc/manuals/R-lang.html states in the following 
section 

4.3.2 Argument matching
This subsection applies to closures but not to primitive functions.

What are 'closures'?
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] What is a 'closure'?

2012-01-19 Thread R. Michael Weylandt
http://www.lemnica.com/esotericR/Introducing-Closures/

Any function you work with will be a closure -- primitives are
built-in functions that users can't create. (without source editing 
recompiling R) E.g. the function c() (type it without parentheses at
the prompt to see its code)

Let me know if I can explain more,

Michael

On Thu, Jan 19, 2012 at 2:45 PM, Ajay Askoolum aa2e...@yahoo.co.uk wrote:
 The R Language Definition at 
 http://cran.r-project.org/doc/manuals/R-lang.html states in the following 
 section

 4.3.2 Argument matching
 This subsection applies to closures but not to primitive functions.

 What are 'closures'?
        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.

__
R-help@r-project.org mailing list
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.


Re: [R] What is a 'closure'?

2012-01-19 Thread Ajay Askoolum
Michael, thank you, especially for the link. I think I understand.

The vocabulary is so different! I know 'closure' as 'user-defined function'.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] What is a 'closure'?

2012-01-19 Thread Ben Bolker
Ajay Askoolum aa2e72e at yahoo.co.uk writes:

 
 Michael, thank you, especially for the link. I think I understand.
 
 The vocabulary is so different! I know 'closure' as 'user-defined function'.
 

  Not quite.  All (??) user-defined functions are closures, but lots
of non-user-defined functions are closures too ...

 typeof(mean)
[1] closure
 typeof(sum)
[1] builtin
 typeof(apply)
[1] closure

__
R-help@r-project.org mailing list
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.


Re: [R] What is a 'closure'?

2012-01-19 Thread Ajay Askoolum


Thanks for clarifying.

Is my (new) understanding stated below correct?

- A closure is any function (user- or system- defined) where 
is.primitive(functionName) is FALSE. 

- is.primitive(functionName) is FALSE when functionName is a system-defined 
function that is coded in R itself.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] What is a 'closure'?

2012-01-19 Thread peter dalgaard

On Jan 19, 2012, at 21:39 , Ben Bolker wrote:

 Ajay Askoolum aa2e72e at yahoo.co.uk writes:
 
 
 Michael, thank you, especially for the link. I think I understand.
 
 The vocabulary is so different! I know 'closure' as 'user-defined function'.
 
 
  Not quite.  All (??) user-defined functions are closures, but lots
 of non-user-defined functions are closures too ...
 

Also, it is not actually the function that is the closure, it is the function 
completed with its environment, which is where, during evaluation, unbound 
objects will be sought.

The function itself is a parsed version of the function definition. When 
called, almost all functions will need to find something from their 
environment, e.g. the - operator. The only functions that are completely 
self-contained are those that return a constant or one of the function 
arguments (maybe a few more). 

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list
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.


Re: [R] What is a 'closure'?

2012-01-19 Thread Bert Gunter
On Thu, Jan 19, 2012 at 1:15 PM, Ajay Askoolum aa2e...@yahoo.co.uk wrote:


 Thanks for clarifying.

 Is my (new) understanding stated below correct?

No.

-- Bert

 - A closure is any function (user- or system- defined) where 
 is.primitive(functionName) is FALSE.

 - is.primitive(functionName) is FALSE when functionName is a system-defined 
 function that is coded in R itself.
        [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
R-help@r-project.org mailing list
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.


Re: [R] What is a 'closure'?

2012-01-19 Thread Hadley Wickham
On Thu, Jan 19, 2012 at 1:45 PM, Ajay Askoolum aa2e...@yahoo.co.uk wrote:
 The R Language Definition at 
 http://cran.r-project.org/doc/manuals/R-lang.html states in the following 
 section

 4.3.2 Argument matching
 This subsection applies to closures but not to primitive functions.

 What are 'closures'?

You might find the discussion here helpful:
https://github.com/hadley/devtools/wiki/First-class-functions

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

__
R-help@r-project.org mailing list
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.


Re: [R] What is a 'closure'?

2012-01-19 Thread R. Michael Weylandt
To put a little more concrete example into what Peter and Ben said
about the environment being key:

environment(c)
 # NULL This is a primitive, so it has no environment

environment(library)
 # environment: namespace:base Built-in function but it has an
environment (usually the namespace of the package whence it comes)

But don't think that's only because library() does all its work at the
R-level while c() works at the C level

environment(as.vector)
# Same as above: this function is implemented as an Internal which
means (basically) it goes straight to the C level as well
# (For completeness sake, it can dispatch as an S3 generic back to R)

So the lesson is -- almost anything is a closure except for things
that are explicitly primitives.

Michael

On Thu, Jan 19, 2012 at 4:19 PM, peter dalgaard pda...@gmail.com wrote:

 On Jan 19, 2012, at 21:39 , Ben Bolker wrote:

 Ajay Askoolum aa2e72e at yahoo.co.uk writes:


 Michael, thank you, especially for the link. I think I understand.

 The vocabulary is so different! I know 'closure' as 'user-defined function'.


  Not quite.  All (??) user-defined functions are closures, but lots
 of non-user-defined functions are closures too ...


 Also, it is not actually the function that is the closure, it is the function 
 completed with its environment, which is where, during evaluation, unbound 
 objects will be sought.

 The function itself is a parsed version of the function definition. When 
 called, almost all functions will need to find something from their 
 environment, e.g. the - operator. The only functions that are completely 
 self-contained are those that return a constant or one of the function 
 arguments (maybe a few more).

 --
 Peter Dalgaard, Professor,
 Center for Statistics, Copenhagen Business School
 Solbjerg Plads 3, 2000 Frederiksberg, Denmark
 Phone: (+45)38153501
 Email: pd@cbs.dk  Priv: pda...@gmail.com

 __
 R-help@r-project.org mailing list
 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.

__
R-help@r-project.org mailing list
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.