Write four R function factories that return closures: a multiplier, a polynomial evaluator, a running accumulator and a counter. It tests how closures capture their enclosing environment, how the <<- operator keeps state between calls, and why lazy arguments need force().
Implement four function factories in R. Each factory returns a function that captures variables from the environment of the factory call.
- `make_multiplier(factor)`: returns `g(x)` giving the numeric vector `x * factor` (vectorized, recycling `factor`).
- `make_polynomial(coeffs)`: returns `p(x)` that evaluates `coeffs[1] + coeffs[2]*x + ... + coeffs[n]*x^(n-1)` element-wise over `x`.
- `make_accumulator()`: returns `add(x)`, which adds `sum(x)` to a running total that starts at 0 and returns the updated total.
- `make_counter(start = 0, step = 1)`: returns a no-argument `tick()`. The value starts at `start`; each call first adds `step` and then returns the new value.
State persists across calls through `<<-`, and each closure has its own independent environment, so two counters do not share state.
Example:
```r
double <- make_multiplier(2)
double(c(1, 2, 3)) # 2 4 6
quad <- make_polynomial(c(1, 0, 1))
quad(c(0, 1, 2)) # 1 2 5 (1 + x^2)
acc <- make_accumulator()
acc(c(1, 2)) # 3
acc(10) # 13
count <- make_counter(start = 0, step = 1)
count() # 1
count() # 2
```
```hint Where assignments land
Work out which environment a plain `<-` inside the returned function writes to, and which environment has to change for the running total to survive between calls.
```
```hint Lazy arguments
R evaluates an argument only when it is first used. Think about what a closure built inside a `for` loop sees if the factory never touches its argument before returning.
```
### What a Strong Answer Covers
- Each factory returns a function, and the captured values live in the environment created by that factory call.
- Correct use of `<<-` for the stateful closures, and why a plain `<-` would not persist.
- Forcing lazily evaluated arguments so each closure keeps the value it was built with.
- Element-wise polynomial evaluation that follows the coefficient order, constant term first.
- Independent state for closures created by separate factory calls.
### Follow-up Questions
- Why does each returned closure keep its own independent state?
- How would you evaluate the polynomial efficiently for large `coeffs`?
- How would you give the counter a `reset()` function that works on the same state as `tick()`?
Overview: Write four R function factories that return closures: a multiplier, a polynomial evaluator, a running accumulator and a counter. It tests how closures capture their enclosing environment, how the <<- operator keeps state between calls, and why lazy arguments need force().
Implement four function factories in R. Each factory returns a function that captures variables from the environment of the factory call.
make_multiplier(factor)
: returns
g(x)
giving the numeric vector
x * factor
(vectorized, recycling
factor
).
make_polynomial(coeffs)
: returns
p(x)
that evaluates
coeffs[1] + coeffs[2]*x + ... + coeffs[n]*x^(n-1)
element-wise over
x
.
make_accumulator()
: returns
add(x)
, which adds
sum(x)
to a running total that starts at 0 and returns the updated total.
make_counter(start = 0, step = 1)
: returns a no-argument
tick()
. The value starts at
start
; each call first adds
step
and then returns the new value.
State persists across calls through <<-, and each closure has its own independent environment, so two counters do not share state.