memoization

Does this Haskell function memoize?

纵然是瞬间 提交于 2019-12-08 02:55:58
问题 I'd like to pre-calculate some values that are then used when I need to do further lookups. I came up with the following: import qualified Data.Vector.Unboxed as V lcgen s = lc where lc 0 b = lengths V.! b lc a b = lengths V.! b - lengths V.! (a - 1) - 1 lengths = V.fromList $ scanl1 ((+) . (1 +)) $ map length $ words s The function essentially returns the number of characters used in-between two words. I use it as follows: let lc = lcgen "some sentence with a lot of words" lc 0 0 -- == 4 lc

Memoize a function so that it isn't reset when I rerun the file in Python

泄露秘密 提交于 2019-12-07 22:42:49
问题 I often do interactive work in Python that involves some expensive operations that I don't want to repeat often. I'm generally running whatever Python file I'm working on frequently. If I write: import functools32 @functools32.lru_cache() def square(x): print "Squaring", x return x*x I get this behavior: >>> square(10) Squaring 10 100 >>> square(10) 100 >>> runfile(...) >>> square(10) Squaring 10 100 That is, rerunning the file clears the cache. This works: try: safe_square except NameError:

real world example of memoization in javascript?

我的梦境 提交于 2019-12-07 13:57:59
问题 I've found examples such as factorial calculation to explain memoization. These are helpful but I'm looking for a deeper understanding. I'm wondering if someone can describe a real world application of this technique and why they used it instead of recursion or whatever else they felt using memoization might help them optimize. 回答1: Memoization is a little more specific than just caching. Think about searching for an element in the DOM using a selector, like you might with jQuery. Say, $('

What is a good way to memoize data across many instances of a class in Ruby?

佐手、 提交于 2019-12-07 12:21:14
问题 Consider: many instances of an object that generates data. It would be great to only generate that data once per run. class HighOfNPeriods < Indicator def generate_data @indicator_data = DataStream.new (0..@source_data.data.count - 1).each do |i| if i < @params[:n_days] ... @indicator_data.add one_data end end There are different instances of HighOfNPeriods with different params and different @source_data . Here is how the indicator is used: class Strategy attr_accessor :indicators def

Memoize a function so that it isn't reset when I rerun the file in Python

一世执手 提交于 2019-12-06 12:26:04
I often do interactive work in Python that involves some expensive operations that I don't want to repeat often. I'm generally running whatever Python file I'm working on frequently. If I write: import functools32 @functools32.lru_cache() def square(x): print "Squaring", x return x*x I get this behavior: >>> square(10) Squaring 10 100 >>> square(10) 100 >>> runfile(...) >>> square(10) Squaring 10 100 That is, rerunning the file clears the cache. This works: try: safe_square except NameError: @functools32.lru_cache() def safe_square(x): print "Squaring", x return x*x but when the function is

Can I avoid Python loop overhead on dynamic programming with numpy?

邮差的信 提交于 2019-12-06 11:29:21
I need help with the Pythonic looping overhead of the following problem: I'm writing a function that calculates a pixel flow algorithm that's a classic dynamic programming algorithm on a 2D Numpy array. It requires: 1) visiting all the elements of the array at least once like this: for x in range(xsize): for y in range(ysize): updateDistance(x,y) 2) potentially following a path of elements based on the values of the neighbors of an element which looks like this while len(workingList) > 0: x,y = workingList.pop() #if any neighbors of x,y need calculation, push x,y and neighbors on workingList

Does this Haskell function memoize?

二次信任 提交于 2019-12-06 09:28:41
I'd like to pre-calculate some values that are then used when I need to do further lookups. I came up with the following: import qualified Data.Vector.Unboxed as V lcgen s = lc where lc 0 b = lengths V.! b lc a b = lengths V.! b - lengths V.! (a - 1) - 1 lengths = V.fromList $ scanl1 ((+) . (1 +)) $ map length $ words s The function essentially returns the number of characters used in-between two words. I use it as follows: let lc = lcgen "some sentence with a lot of words" lc 0 0 -- == 4 lc 0 1 -- == 13 In this implementation, would the lengths vector be memoized? Furthermore, how can I know

How is this JavaScript function caching its results?

最后都变了- 提交于 2019-12-06 04:17:28
问题 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

memoize continuation passing style function

谁说胖子不能爱 提交于 2019-12-06 02:41:39
问题 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

real world example of memoization in javascript?

人走茶凉 提交于 2019-12-05 20:41:21
I've found examples such as factorial calculation to explain memoization . These are helpful but I'm looking for a deeper understanding. I'm wondering if someone can describe a real world application of this technique and why they used it instead of recursion or whatever else they felt using memoization might help them optimize. Memoization is a little more specific than just caching. Think about searching for an element in the DOM using a selector, like you might with jQuery. Say, $('.some-selector') . In this context, I'm calling the function $ , telling it to find for me all elements that