Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Created February 14, 2020 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewstokeley/9001abe535195fb7ed4e7ecd211acba0 to your computer and use it in GitHub Desktop.
Save matthewstokeley/9001abe535195fb7ed4e7ecd211acba0 to your computer and use it in GitHub Desktop.
pure-function-patterns

/**
 * ht vue, lo
 * Cache a function
 */
function cached (
  fn: Function,
  resolver: Function
): Function {
   const cache = new WeakMap()
   return (function(str) {
     const key = resolver ? resolver(str) : str
     return cache.has(key) 
       ? cache.get(key)
       : cache.set(key, fn(str))
    })
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment