Re: [Rd] callCC in 2.7.0

2008-04-02 Thread Luke Tierney
On Tue, 1 Apr 2008, Dr. Ferdinand Jamitzky wrote:

> Anyway something like "yield" would be very nice to have.
> Would it be hard to implement?

Short answer: yes, it would be hard.

Longer answer: R's interpreter is based on lots of recursive C calls.
There are standard ways of implementing continuations in that sort of
context, but they are not entirely portable (involves C stack
copying), require assembly language programming on some platforms
(register window flushing on sparc for example) and can be fairly
inefficient.  It might be possible to implement something more
restricive that is sufficient for implementing generators, but I don't
think it would be much easier given the current architecture.  It is
worth keeping this in mind if we make changes to the evaluator, but
that is not likely to happen real soon.  Some of us worked on
something along these lines a while back in a branch called R-uthreads,
but the cost of converting to that approach and the overhead of
maintaining it seemed too high for the benefit, at least at the time.

Best,

luke

> It could be in a similar way to scala where you can define for-comprehensions.
> I really miss them in R.
> best wishes
> ferdinand
>
> On Tue, Apr 1, 2008 at 12:32 PM, Luke Tierney <[EMAIL PROTECTED]> wrote:
>> No.  First class continuations of the kind provided in scheme can be
>>  used as a means to implement generators, but downward-only
>>  continuations as currently provided in R are not sufficient for that.
>>  This version is intended only as a non-local exit mechanism.
>>
>>  Best,
>>
>>  luke
>>
>>  On Mon, 31 Mar 2008, f.jamitzky wrote:
>>
>> >
>> > callcc is similar to the "yield" keyword in python and c#
>> > it lets you define e.g. a generator of lists of numbers.
>> >
>> >
>> >
>> >
>> > Gabor Grothendieck wrote:
>> >>
>> >> Would anyone like to explain if callCC in R 2.7.0 gives
>> >> anything that on.exit does not already provide?
>> >>
>> >> It seems that the exit condition once defined cannot
>> >> be added to overridden whereas with on.exit multiple
>> >> on.exit's add additional on.exits rather than being ignored.
>> >>
>> >> Is this important?
>> >>
>> >> __
>> >> R-devel@r-project.org mailing list
>> >> https://stat.ethz.ch/mailman/listinfo/r-devel
>> >>
>> >>
>> >
>> >
>>
>>  --
>>  Luke Tierney
>>  Chair, Statistics and Actuarial Science
>>  Ralph E. Wareham Professor of Mathematical Sciences
>>  University of Iowa  Phone: 319-335-3386
>>  Department of Statistics andFax:   319-335-3017
>> Actuarial Science
>>  241 Schaeffer Hall  email:  [EMAIL PROTECTED]
>>  Iowa City, IA 52242 WWW:  http://www.stat.uiowa.edu
>>
>
>
>
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa  Phone: 319-335-3386
Department of Statistics andFax:   319-335-3017
Actuarial Science
241 Schaeffer Hall  email:  [EMAIL PROTECTED]
Iowa City, IA 52242 WWW:  http://www.stat.uiowa.edu

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] "[<-" plus drop-type extra argument

2008-04-02 Thread Martin Morgan

Sklyar, Oleg (MI London) wrote:
> Robin:
> 
> two points:

> Second, your setReplaceMethod seems to be wrong in the definition, try the 
> following adding signature for value and ... in the function call. You cannot 
> simply drop the ... . Probably you will also need to include j="missing" in 
> the signature. There is also no need to construct a new object on return and 
> I do not think drop is a part of generic for replacement, it is for 
> extraction though
> 
> setReplaceMethod("[", signature(x="foo", value="ANY"),
>   function(x, i,j, ..., recalculate=FALSE, value) {
> [EMAIL PROTECTED] = value
> if (recalculate) [EMAIL PROTECTED] = sum([EMAIL PROTECTED])
> x
>   })

Informative to investigate this a bit further. Without the ... we have

 > showMethods("[<-", class="foo", includeDef=TRUE)
Function: [<- (package base)
x="foo"
function (x, i, j, ..., value)
{
 .local <- function (x, i, j, recalculate = FALSE, value)
 x
 .local(x, i, j, ..., value)
}

.local maps between the generic and the method signature; the map is 
incorrect when there are additional arguments but no  An 
indiscretion, perhaps, on the part of the methods package. This is in 
contrast to a method with ...

 > showMethods("[<-", class="foo", includeDef=TRUE)
Function: [<- (package base)
x="foo"
function (x, i, j, ..., value)
{
 .local <- function (x, i, j, ..., recalculate = FALSE, value)
 x
 .local(x, i, j, ..., value = value)
}

here .local plays the same role, but value is named explicitly and the 
problem avoided.

With respect to use of 'new' and slot assignments,  it might be delving 
too much into implementation but I think (a) using 'initialize' is 
better than 'new' when you're doing a copy construction and (b) 
'initialize' is more memory efficient than slot assignment when doing 
multiple assignments (at least tracemem says so):

setGeneric("up1", function(x) standardGeneric("up1"))
setGeneric("up2", function(x) standardGeneric("up2"))

setMethod("up1", signature(x="foo"), function(x) {
 [EMAIL PROTECTED] <- 1
 [EMAIL PROTECTED] <- NA_real_
 x
})
setMethod("up2", signature(x="foo"), function(x) {
 initialize(x, x=1, NC=NA_real_)
})

 > a1 <- up1(a) # 2 copies
tracemem[0x6d08d60 -> 0x7fa4628]: up1 up1
tracemem[0x7fa4628 -> 0x9b45518]: up1 up1
 > a2 <- up2(a) # just 1
tracemem[0x6d08d60 -> 0x6cad360]: initialize initialize up2 up2
 > identical(a1, a2)
[1] TRUE

This also provides object validation, which may or may not be desirable.

Martin

> Here is the output:
>> a = new("foo", 1:15)
>> a
> An object of class “foo”
>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
> Slot "NC":
> [1] NA
> 
>> a[5,recalculate=TRUE] = 25
>> a
> An object of class “foo”
>  [1]  1  2  3  4 25  6  7  8  9 10 11 12 13 14 15
> Slot "NC":
> [1] 140
> 
> 
> Dr Oleg Sklyar
> Technology Group
> Man Investments Ltd
> +44 (0)20 7144 3803
> [EMAIL PROTECTED] 
> 
>> -Original Message-
>> From: [EMAIL PROTECTED] 
>> [mailto:[EMAIL PROTECTED] On Behalf Of Robin Hankin
>> Sent: 02 April 2008 09:51
>> To: R-devel@r-project.org
>> Subject: [Rd] "[<-" plus drop-type extra argument
>>
>> Hello
>>
>> I am writing a replacement method for an S4 class and want to 
>> pass an additional argument to  "[<-"() along the lines of  
>> "["()'s  "drop"  
>> argument.
>>
>> Specifically, I have an S4  class, call it "foo", with a slot  'x'  
>> that is a
>> vector and a slot  'NC' that  is a scalar.
>>
>> I want to be able to pass a Boolean argument to the 
>> replacement method which specifies whether or not to 
>> recalculate  NC (which is time-consuming and often not 
>> needed).  I want the default behaviour to be "don't recalculate NC".
>>
>> Toy example follows, in which 'NC' is the sum of x (in my 
>> application, calculating NC is an expensive multidimensional 
>> integral).
>>
>>
>> setClass("foo",
>>   representation = representation(x="numeric" , NC="numeric"),
>>   prototype  = list(x=double() , NC=NA_real_)
>>   )
>>
>> setReplaceMethod("[",signature(x="foo"),
>>   function(x,i,j,recalculate=FALSE,value){
>> jj <- [EMAIL PROTECTED]
>> jj[i] <- value
>> if(recalculate){
>>   return(new("foo" , x=jj , NC=sum(jj)))
>> } else {
>>   return(new("foo" , x=jj , NC=NA_real_))
>> }
>>   }
>>   )
>>
>>
>>
>>
>> Then
>>
>>
>>
>>  >
>>  > a <- new("foo", x=1:10,NC=45)
>>
>>
>>  > a[4,recalculate=FALSE] <- 1
>>  > a
>> An object of class “foo”
>> Slot "x":
>>   [1] 1 2 3 1 5 6 7 8 910
>>
>> Slot "NC":
>> [1] NA
>>
>> #  Desired behaviour: NC not recalculated
>>
>>
>>  >
>>  > a[4,recalculate=TRUE] <- 1
>>  > a
>> An object of class “foo”
>> Slot "x":
>>   [1] 1 2 3 1 5 6 7 8 910
>>
>> Slot "NC":
>>

Re: [Rd] "[<-" plus drop-type extra argument

2008-04-02 Thread Sklyar, Oleg (MI London)
Robin:

two points:

first for convenience and performance it has sense to derive your class from 
numeric rather than introduce a numeric slot, i.e.

setClass("foo",
  representation(
"numeric",
NC="numeric"
  ),
  prototype(
NC=NA_real_
  )
)

or

setClass("foo",
  representation(
NC="numeric"
  ),
  prototype(
NC=NA_real_
  ),
  contains="numeric"
)

In this way your data is in the .Data slot by default and all the functions 
defined for numeric work out of the box (you just need to redefine those that 
have to return foo rather than numeric).

Second, your setReplaceMethod seems to be wrong in the definition, try the 
following adding signature for value and ... in the function call. You cannot 
simply drop the ... . Probably you will also need to include j="missing" in the 
signature. There is also no need to construct a new object on return and I do 
not think drop is a part of generic for replacement, it is for extraction though

setReplaceMethod("[", signature(x="foo", value="ANY"),
  function(x, i,j, ..., recalculate=FALSE, value) {
[EMAIL PROTECTED] = value
if (recalculate) [EMAIL PROTECTED] = sum([EMAIL PROTECTED])
x
  })

Here is the output:
> a = new("foo", 1:15)
> a
An object of class “foo”
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
Slot "NC":
[1] NA

> a[5,recalculate=TRUE] = 25
> a
An object of class “foo”
 [1]  1  2  3  4 25  6  7  8  9 10 11 12 13 14 15
Slot "NC":
[1] 140


Dr Oleg Sklyar
Technology Group
Man Investments Ltd
+44 (0)20 7144 3803
[EMAIL PROTECTED] 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Robin Hankin
> Sent: 02 April 2008 09:51
> To: R-devel@r-project.org
> Subject: [Rd] "[<-" plus drop-type extra argument
> 
> Hello
> 
> I am writing a replacement method for an S4 class and want to 
> pass an additional argument to  "[<-"() along the lines of  
> "["()'s  "drop"  
> argument.
> 
> Specifically, I have an S4  class, call it "foo", with a slot  'x'  
> that is a
> vector and a slot  'NC' that  is a scalar.
> 
> I want to be able to pass a Boolean argument to the 
> replacement method which specifies whether or not to 
> recalculate  NC (which is time-consuming and often not 
> needed).  I want the default behaviour to be "don't recalculate NC".
> 
> Toy example follows, in which 'NC' is the sum of x (in my 
> application, calculating NC is an expensive multidimensional 
> integral).
> 
> 
> setClass("foo",
>   representation = representation(x="numeric" , NC="numeric"),
>   prototype  = list(x=double() , NC=NA_real_)
>   )
> 
> setReplaceMethod("[",signature(x="foo"),
>   function(x,i,j,recalculate=FALSE,value){
> jj <- [EMAIL PROTECTED]
> jj[i] <- value
> if(recalculate){
>   return(new("foo" , x=jj , NC=sum(jj)))
> } else {
>   return(new("foo" , x=jj , NC=NA_real_))
> }
>   }
>   )
> 
> 
> 
> 
> Then
> 
> 
> 
>  >
>  > a <- new("foo", x=1:10,NC=45)
> 
> 
>  > a[4,recalculate=FALSE] <- 1
>  > a
> An object of class “foo”
> Slot "x":
>   [1] 1 2 3 1 5 6 7 8 910
> 
> Slot "NC":
> [1] NA
> 
> #  Desired behaviour: NC not recalculated
> 
> 
>  >
>  > a[4,recalculate=TRUE] <- 1
>  > a
> An object of class “foo”
> Slot "x":
>   [1] 1 2 3 1 5 6 7 8 910
> 
> Slot "NC":
> [1] 10051
> 
> 
> # Desired behaviour: NC recalculated
> 
>  >
>  > a[4] <- 1
> Error in .local(x, i, j, ..., value) :
>argument "value" is missing, with no default  >
> 
> # Undesired behaviour:  I wanted 'recalculate' to take its 
> default value of FALSE, and 'NC' not be recalculated.
> 
> 
> How to do this?
> 
> 
> 
> 
> 
> --
> Robin Hankin
> Uncertainty Analyst and Neutral Theorist, National 
> Oceanography Centre, Southampton European Way, Southampton 
> SO14 3ZH, UK
>   tel  023-8059-7743
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 


**
The contents of this email are for the named addressee(s) only.
It contains information which may be confidential and privileged.
If you are not the intended recipient, please notify the sender
immediately, destroy this email and any attachments and do not
otherwise disclose or use them. Email transmission is not a
secure method of communication and Man Investments cannot accept
responsibility for the completeness or accuracy of this email or
any attachments. Whilst Man Investments makes every effort to keep
its network free from viruses, it does not accept responsibility
for any computer virus which might be transferred by way of this
email or any attachments. This email does not constitute a request,
o

[Rd] "[<-" plus drop-type extra argument

2008-04-02 Thread Robin Hankin
Hello

I am writing a replacement method for an S4 class and want to pass
an additional argument to  "[<-"() along the lines of  "["()'s  "drop"  
argument.

Specifically, I have an S4  class, call it "foo", with a slot  'x'  
that is a
vector and a slot  'NC' that  is a scalar.

I want to be able to pass a Boolean argument to the replacement
method which specifies whether or not to recalculate  NC (which
is time-consuming and often not needed).  I want the default behaviour
to be "don't recalculate NC".

Toy example follows, in which 'NC' is the sum of x (in my application,
calculating NC is an expensive multidimensional integral).


setClass("foo",
  representation = representation(x="numeric" , NC="numeric"),
  prototype  = list(x=double() , NC=NA_real_)
  )

setReplaceMethod("[",signature(x="foo"),
  function(x,i,j,recalculate=FALSE,value){
jj <- [EMAIL PROTECTED]
jj[i] <- value
if(recalculate){
  return(new("foo" , x=jj , NC=sum(jj)))
} else {
  return(new("foo" , x=jj , NC=NA_real_))
}
  }
  )




Then



 >
 > a <- new("foo", x=1:10,NC=45)


 > a[4,recalculate=FALSE] <- 1
 > a
An object of class “foo”
Slot "x":
  [1] 1 2 3 1 5 6 7 8 910

Slot "NC":
[1] NA

#  Desired behaviour: NC not recalculated


 >
 > a[4,recalculate=TRUE] <- 1
 > a
An object of class “foo”
Slot "x":
  [1] 1 2 3 1 5 6 7 8 910

Slot "NC":
[1] 10051


# Desired behaviour: NC recalculated

 >
 > a[4] <- 1
Error in .local(x, i, j, ..., value) :
   argument "value" is missing, with no default
 >

# Undesired behaviour:  I wanted 'recalculate' to take its default  
value of FALSE, and 'NC' not be recalculated.


How to do this?





--
Robin Hankin
Uncertainty Analyst and Neutral Theorist,
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel