memoization

Memoization in OCaml?

情到浓时终转凉″ 提交于 2019-12-18 17:13:35
问题 It is possible to improve "raw" Fibonacci recursive procedure Fib[n_] := If[n < 2, n, Fib[n - 1] + Fib[n - 2]] with Fib[n_] := Fib[n] = If[n < 2, n, Fib[n - 1] + Fib[n - 2]] in Wolfram Mathematica. First version will suffer from exponential explosion while second one will not since Mathematica will see repeating function calls in expression and memoize (reuse) them. Is it possible to do the same in OCaml? How to improve let rec fib n = if n<2 then n else fib (n-1) + fib (n-2);; in the same

How to memoize recursive functions?

拥有回忆 提交于 2019-12-18 15:51:33
问题 Consider a recursive function, say the Euclid algorithm defined by: let rec gcd a b = let (q, r) = (a / b, a mod b) in if r = 0 then b else gcd b r (This is a simplified, very brittle definition.) How to memoize such a function? The classical approach of defining a high-order function memoize : ('a -> 'b) -> ('a -> 'b) adding memoization to the function is here useless, because it will only save time on the first call. I have found details on how to memoize such function in Lisp or Haskell:

What is the lifetime of a memoized value in a functional language like Haskell?

南笙酒味 提交于 2019-12-18 12:15:45
问题 In a pure functional language with lazy semantics (such as Haskell), results of computations are memoized so that further evaluations of a function with the same inputs do not recompute the value but get it directly from the cache of memoized values. I am wondering if these memoized values get recycled at some point in time? If so, it means that the memoized values must be recomputed at a later time, and the benefits of memoization are not so exiting IMHO. If not, then ok, this is clever to

memoize to disk - python - persistent memoization

我怕爱的太早我们不能终老 提交于 2019-12-18 11:18:46
问题 Is there a way to memoize the output of a function to disk? I have a function def getHtmlOfUrl(url): ... # expensive computation and would like to do something like: def getHtmlMemoized(url) = memoizeToFile(getHtmlOfUrl, "file.dat") and then call getHtmlMemoized(url), so as to do the expensive computation only once for each url. 回答1: Python offers a very elegant way to do this - decorators. Basically, a decorator is a function that wraps another function to provide additional functionality

What are the different techniques for memoization in Java? [closed]

时间秒杀一切 提交于 2019-12-17 17:33:41
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I know of this one http://onjava.com/pub/a/onjava/2003/08/20/memoization.html but is there anything else? 回答1: Memoization is also easy with plain simple typesafe Java. You can do it from scratch with the following reusable classes. I use these as caches whose lifespan are the

Memoization of promise-based function

可紊 提交于 2019-12-17 14:44:24
问题 How can I memoize a promise-based function? Would straightforward memoization of the function suffice? function foo() { return new Promise((resolve, reject) => { doSomethingAsync({ success: resolve, fail: reject }); }); }; Would this suffice? var fooMemoized = memoize(foo); Note: this question has been updated to remove the deferred anti-pattern. 回答1: Yes, that will suffice. Promises are simple return values, which is their great benefit - in contrast to callbacks, where memoisation code

Caching class attributes in Python

冷暖自知 提交于 2019-12-17 10:25:33
问题 I'm writing a class in python and I have an attribute that will take a relatively long time to compute, so I only want to do it once . Also, it will not be needed by every instance of the class, so I don't want to do it by default in __init__ . I'm new to Python, but not to programming. I can come up with a way to do this pretty easily, but I've found over and over again that the 'Pythonic' way of doing something is often much simpler than what I come up with using my experience in other

What type to use to store an in-memory mutable data table in Scala?

坚强是说给别人听的谎言 提交于 2019-12-17 06:08:59
问题 Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values. How do I best implement this? Arguments are of diverse types, including some enums. In C# I'd generally use DataTable. Is there an equivalent in Scala? 回答1: You could use a mutable.Map[TupleN[A1, A2, ..., AN], R] , or if memory is a concern, a WeakHashMap [1]. The

What type to use to store an in-memory mutable data table in Scala?

假如想象 提交于 2019-12-17 06:06:03
问题 Each time a function is called, if it's result for a given set of argument values is not yet memoized I'd like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values. How do I best implement this? Arguments are of diverse types, including some enums. In C# I'd generally use DataTable. Is there an equivalent in Scala? 回答1: You could use a mutable.Map[TupleN[A1, A2, ..., AN], R] , or if memory is a concern, a WeakHashMap [1]. The

memoization library for python 2.7

浪尽此生 提交于 2019-12-17 04:24:09
问题 I see that python 3.2 has memoization as a decorator in functools library. http://docs.python.org/py3k/library/functools.html#functools.lru_cache Unfortunately it is not yet backported to 2.7. Is there any specific reason as why it is not available in 2.7? Is there any 3rd party library providing the same feature or should I write my own? 回答1: Is there any specific reason as why it is not available in 2.7? @Nirk has already provided the reason: unfortunately, the 2.x line only receive