Yes, memoize does not change its argument, so
recreates the memoized function for each call.
(fn [] ((memoize rand-int) 10))
equivalently.
To call a memoized function from another function, you need to put it in Var or a private local:
(repeatedly 10 (let [r (memoize rand-int)] #(r 10))) ;= (2 2 2 2 2 2 2 2 2 2)
In the partial example, the function returned (memoize rand-int) is passed as an argument to the partial function, which then returns a closure that closes on return (memoize rand-int) . So, this is very close to the above example (except for the closure returned by partial , it uses apply to call the memoized function).
source share