Replace the value at a mutable memory location using a closure