memoization

How is this JavaScript function caching its results?

心已入冬 提交于 2019-12-04 10:07:40
After reading over it several times, I still don't understand how this example code from page 76 of Stoyan Stefanov's "JavaScript Patterns" works. I'm not a ninja yet. But to me, it reads like it's only storing an empty object: var myFunc = function (param) { if (!myFunc.cache[param]) { var result = {}; // ... expensive operation ... myFunc.cache[param] = result; } return myFunc.cache[param]; }; // cache storage myFunc.cache = {}; Unless that unseen "expensive operation" is storing back to result , I don't see anything being retained. Where are the results being stored? P.S.: I've read Caching

Memoize a currified function

天大地大妈咪最大 提交于 2019-12-04 08:56:12
问题 const f = (arg1) => (arg2) => { /* returns something */ } Is it possible to memoize f with regard to the 2 arguments, namely: f(1)(2); f(1)(3); // Cache not hit f(4)(2); // Cache not hit f(1)(2); // Cache hit 回答1: You could take a Map as cache and take nested maps for all following arguments. This cache works for arbitrary count of arguments and reuses the values from the former calls. It works by taking a curried function and an optional Map . If the map is not supplied, a new map is created

memoize continuation passing style function

北战南征 提交于 2019-12-04 07:49:49
I'm wondering if there is a way to implement a generic "memoize" functional (as in a function with a function as input and a function as output, as python's decorators) capable of handling also cps-style functions. for a normal function (as in "the result value comes back by the return, the parameters are only for input!") a memoize function can be as simple as (in javascript) function memoize(fun) { var cache = {}; return function () { var args = Array.prototype.slice.call(arguments); if (args in cache) return cache[args]; var ret = fun.apply(this, arguments); cache[args] = ret; return ret; }

Implementing Automatic Memoization (returns a closured function) in JavaScript

好久不见. 提交于 2019-12-04 06:30:09
问题 I've read http://www.sitepoint.com/implementing-memoization-in-javascript/ Automatic Memoization In all of the previous examples, the functions were explicitly modified to add memoization. It is also possible to implement a memoization infrastructure without modifying the functions at all. This is useful because it allows the function logic to be implemented separately from the memoization logic. This is done by creating a utility function which takes a function as input and applies

Reentrant caching of “referentially transparent” IO calls

我怕爱的太早我们不能终老 提交于 2019-12-04 06:27:51
Assume we have an IO action such as lookupStuff :: InputType -> IO OutputType which could be something simple such as DNS lookup, or some web-service call against a time-invariant data. Let's assume that: The operation never throws any exception and/or never diverges If it wasn't for the IO monad, the function would be pure, i.e. the result is always the same for equal input parameters The action is reentrant, i.e. it can be called from multiple threads at the same time safely. The lookupStuff operation is quite (time-)expensive. The problem I'm facing is how to properly (and w/o using any

Efficient algorithm to get the combinations of all items in object

筅森魡賤 提交于 2019-12-04 05:11:24
Given an array or object with n keys, I need to find all combinations with length x . Given X is variable. binomial_coefficient(n,x) . Currently I'm using this: function combine(items) { var result = []; var f = function(prefix, items) { for (var i = 0; i < items.length; i++) { result.push(prefix + items[i]); f(prefix + items[i], items.slice(i + 1)); } } f('', items); return result; } var combinations = combine(["a", "b", "c", "d"]); The output is: ["a", "ab", "abc", "abcd", "abd", "ac", "acd", "ad", "b", "bc", "bcd", "bd", "c", "cd", "d"] So if I want the binomial coefficient x=3 from n=4 I

Finding first pair of numbers in array that sum to value

笑着哭i 提交于 2019-12-04 04:12:54
问题 Im trying to solve the following Codewars problem: https://www.codewars.com/kata/sum-of-pairs/train/python Here is my current implementation in Python: def sum_pairs(ints, s): right = float("inf") n = len(ints) m = {} dup = {} for i, x in enumerate(ints): if x not in m.keys(): m[x] = i # Track first index of x using hash map. elif x in m.keys() and x not in dup.keys(): dup[x] = i for x in m.keys(): if s - x in m.keys(): if x == s-x and x in dup.keys(): j = m[x] k = dup[x] else: j = m[x] k = m

Containers for different signature functions

纵然是瞬间 提交于 2019-12-04 04:07:29
问题 I'm trying to programming in C++ a framework where the user can indicates a set of functions inside its program where he wants to apply a memoization strategy. So let's suppose that we have 5 functions in our program f1...f5 and we want to avoid the (expensive) re-computation for the functions f1 and f3 if we already called them with the same input. Notice that each function can have different return and argument types. I found this solution for the problem, but you can use only double and

Project Euler 14: performance compared to C and memoization

让人想犯罪 __ 提交于 2019-12-04 00:11:26
问题 I'm currently working on project euler problem 14. I solved it using a poorly coded program, without memoization, that took 386 5 seconds to run (see edit). Here it is: step :: (Integer, Int) -> Integer -> (Integer, Int) step (i, m) n | nextValue > m = (n, nextValue) | otherwise = (i, m) where nextValue = syr n 1 syr :: Integer -> Int -> Int syr 1 acc = acc syr x acc | even x = syr (x `div` 2) (acc + 1) | otherwise = syr (3 * x + 1) (acc + 1) p14 = foldl step (0, 0) [500000..999999] My

Two argument Memoization

Deadly 提交于 2019-12-03 21:23:48
问题 In C# how do I memoize a function with two arguments? Do I have to curry before memoization? Wes Dyer wrote the Memoization code I typically use, but now I need two arguments 回答1: You just make an overloaded version of the Memoize method that has three generic types and takes a function with two parameters, and the two arguments. It still returns a parameterless function: public static Func<R> Memoize<A1,A2,R>(this Func<A1,A2,R> f, A1 a1, A2 a2) { R value = default(R); bool hasValue = false;