Yes, memoize
doesn't modify its argument in any way, so
#((memoize rand-int) 10)
recreates the memoized function on each call.
(fn [] ((memoize rand-int) 10))
is equivalent.
To call a memoized function from another function, you need to put it in a Var or a closed-over 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 by (memoize rand-int)
is passed as an argument to the function partial
, which then returns a closure which closes over the return of (memoize rand-int)
. So, this is very close to the example above (except the closure returned by partial
uses apply
to call the memoized function).