1

I'm trying to define a list of functions in a loop, like so:

rr <- list(); ss <- list(list(1,2),list(3,4))

for (i in 1:length(ss)) {
  A <- ss[[i]][1]
  B <- ss[[i]][2]
  print(paste("A, B:", A, ",", B))
  rr[[i]] <- function () { force(B); B }
}

The print statement gives:

[1] "A, B: 1 , 2"
[1] "A, B: 3 , 4"

...which is what I want! But, when I try to use the functions, I get the wrong answer (A and B are lazily evaluated and wait until the function is actually called to get their values, I think):

> environment(rr[[1]])$A
[[1]]
[1] 3

> rr[[1]]()
[[1]]
[1] 4

> environment(rr[[2]])$A
[[1]]
[1] 3

> rr[[2]]()
[[1]]
[1] 4

How can I rewrite the above for loop so that each function rr[[i]] has its own internal set of parameters, which won't be modified by later rr[[n > i]] function definitions?

2
  • I don't understand, do you want both A and B to be internal parameters of the functions? The above code only makes B in the functions. Dec 2, 2018 at 11:52
  • No I wanted to dynamically define functions within a loop but I was running into errors because the parameters are global, and when the last function is defined, all of the previous functions are affected by the redefinition of that global variable within their environment. I figured out what I wanted to do, anyway -- see below:
    – awwsmm
    Dec 2, 2018 at 11:57

2 Answers 2

1

Remember fortunes::fortune(106). There's certainly a better way to do this.

One way is to make a "function factory" to capture the values. For example,

make_fn <- function(A, B) {
  force(A)
  force(B)
  function(x) A <= x & x <= B  
}

Then your loop would be

for (i in 1:length(ss)) {
  A <- ss[[i]][1]
  B <- ss[[i]][2]
  print(paste("A, B:", A, ",", B))
  rr[[i]] <- make_fn(A, B)
}

Edited to add response to comment: The difference here with force() is as follows: force() applies to promises, i.e. arguments of functions. In your example, B was never an argument to any function, so it was never a promise, and force(B) does nothing except return its value. In my answer, A and B are arguments to make_fn, so when it is evaluated they are promises, and force() causes their expressions to be evaluated and their values to be fixed.

1
  • This is a much better solution than my answer. Not sure why force works in the external function make_fn and not within the definition of rr[[i]] itself, but I'll take it.
    – awwsmm
    Dec 2, 2018 at 13:19
1

Edit: my solution below might help someone so I'll leave it here for posterity, but user2554330's answer is a much better solution for my use-case.


Aha! I figured it out, with some help from this answer:

rr <- list(); ss <- list(list(1,2),list(3,4))

for (i in 1:length(ss)) {
  rr[[i]] <- function(x){}
  body(rr[[i]]) <- parse(text={
    paste("(", ss[[i]][[1]], "<= x) && (x <=", ss[[i]][[2]], ")")
  })
}

> rr[[1]](1.5) # within range [1,2]
[1] TRUE
> rr[[1]](2.5)
[1] FALSE

> rr[[2]](3.5) # within range [3,4]
[1] TRUE
> rr[[2]](4.5)
[1] FALSE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.