Created
November 7, 2014 11:50
-
-
Save aramk/21a6d2da746ad4765dbe to your computer and use it in GitHub Desktop.
Support for object arguments in _.memoize()
This file contains 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
# Adds support for using object arguments with _.memoize(). | |
nextMemoizeObjKey = 1 | |
memoizeObjectArg = (func) -> | |
_.memoize func, (obj) -> | |
key = obj._memoizeObjKey | |
unless key? | |
key = obj._memoizeObjKey = nextMemoizeObjKey++ | |
key | |
# Example: | |
fooFunc = memoizeObjectArg (obj) -> Object.keys(obj) | |
arg = {foo: 'bar'} | |
fooFunc(arg) # => ['foo'] | |
# The same memoized value is returned regardless of changes. | |
arg.foo = 'bar2' | |
fooFunc(arg) # => ['foo'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment