Created
January 28, 2014 22:38
-
-
Save jonahkagan/8678095 to your computer and use it in GitHub Desktop.
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
_.mixin assoc: (assoc_list, key, val) -> assoc_list.concat [[key, val]] | |
_.mixin lookup: (assoc_list, key) -> _.find(assoc_list, ([k, v]) -> k is key)?[1] | |
# Memoizes a one-arg function using JS value equality to check for argument | |
# equality. This is useful if you want to memoize a function that takes | |
# objects. It's slower though, since it has to search for seen args in an array | |
# (which takes time proportional to the number of previously seen args). | |
_.mixin memoizeByEquality: (f) -> | |
seen = [] | |
(arg) -> | |
if (res = _.lookup seen, arg)? | |
res | |
else | |
new_res = f arg | |
seen = _.assoc seen, arg, new_res | |
new_res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment