Skip to content

Instantly share code, notes, and snippets.

@jonahkagan
Created January 28, 2014 22:38
Show Gist options
  • Save jonahkagan/8678095 to your computer and use it in GitHub Desktop.
Save jonahkagan/8678095 to your computer and use it in GitHub Desktop.
_.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