Last active
August 29, 2015 14:11
-
-
Save klmr/4dca6c5b9e72dc4a3a51 to your computer and use it in GitHub Desktop.
Memoize R functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
memoize = function (f) { | |
force(f) | |
cache = new.env() | |
function (...) { | |
key = capture.output(dput(list(...))) | |
if (key %in% ls(cache)) | |
return(get(key, envir = cache)) | |
value = f(...) | |
assign(key, value, envir = cache) | |
value | |
} | |
} | |
# Usage: | |
very_expensive_function = memoize(very_expensive_function) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(Totally untested, unadulterated braindump.)