memoization

Automatic memoizing in functional programming languages

假如想象 提交于 2019-12-28 12:00:20
问题 I always thought that Haskell would do some sort of automatic intelligent memoizing. E.g., the naive Fibonacci implementation fib 0 = 0 fib 1 = 1 fib n = fib (n-2) + fib (n-1) would be fast because of that. Now I read this and it seems I was wrong -- Haskell doesn't seem to do automatic memoization. Or do I understand something wrong? Are there other languages which do automatic (i.e. implicit, not explicit) memoization? What are common ways to implement memoization? In all sample

C# Memoization of functions with arbitrary number of arguments [closed]

牧云@^-^@ 提交于 2019-12-28 03:17:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I'm trying to create a memoization interface for functions with arbitrary number of arguments, but I'm failing miserably I feel like my solution is not very flexible. I tried to define an interface for a function which gets memoized automatically upon execution and each function will have to implement this

Is there a generic way to memoize in Scala?

寵の児 提交于 2019-12-28 02:29:29
问题 I wanted to memoize this: def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2) println(fib(100)) // times out So I wrote this and this surprisingly compiles and works (I am surprised because fib references itself in its declaration): case class Memo[A,B](f: A => B) extends (A => B) { private val cache = mutable.Map.empty[A, B] def apply(x: A) = cache getOrElseUpdate (x, f(x)) } val fib: Memo[Int, BigInt] = Memo { case 0 => 0 case 1 => 1 case n => fib(n-1) + fib(n-2) } println(fib(100)) //

Synchronizing on function parameter for multithreaded memoization

左心房为你撑大大i 提交于 2019-12-25 05:52:33
问题 My core question is: how can I implement synchronization in a method on the combination of the object instance and the method parameter? Here are the details of my situation. I'm using the following code to implement memoization, adapted from this answer: /** * Memoizes a unary function * @param f the function to memoize * @tparam T the argument type * @tparam R the result type */ class Memoized[-T, +R](f: T => R) extends (T => R) { import scala.collection.mutable private[this] val cache =

Synchronizing on function parameter for multithreaded memoization

馋奶兔 提交于 2019-12-25 05:51:50
问题 My core question is: how can I implement synchronization in a method on the combination of the object instance and the method parameter? Here are the details of my situation. I'm using the following code to implement memoization, adapted from this answer: /** * Memoizes a unary function * @param f the function to memoize * @tparam T the argument type * @tparam R the result type */ class Memoized[-T, +R](f: T => R) extends (T => R) { import scala.collection.mutable private[this] val cache =

Redux and Reselect: Using derived data to compute a part of the state

こ雲淡風輕ζ 提交于 2019-12-25 03:55:28
问题 What is the best approach to use one part of the redux state tree to update another part of the state. In particular, I have a simplified flashcard app example below. We have a list of words maintained by one reducer, and then another part of the state tree is maintained by a quiz reducer, which holds the currently chosen word. I would like the quiz reducer to have access to the list of words, and maybe even a computed (memoized) filtered subset of the words. What is the best approach here? /

Redux and Reselect: Using derived data to compute a part of the state

99封情书 提交于 2019-12-25 03:55:05
问题 What is the best approach to use one part of the redux state tree to update another part of the state. In particular, I have a simplified flashcard app example below. We have a list of words maintained by one reducer, and then another part of the state tree is maintained by a quiz reducer, which holds the currently chosen word. I would like the quiz reducer to have access to the list of words, and maybe even a computed (memoized) filtered subset of the words. What is the best approach here? /

Scoping issue memoising bash function with associative array

孤街醉人 提交于 2019-12-24 21:08:46
问题 I have a bash script which uses jq to look up 'dependency' data in some JSON, and take the closure (find dependencies of dependencies of dependencies, etc.). This works fine, but can be very slow, since it may look up the same dependencies over and over, so I'd like to memoise it. I tried using a global associative array to associate arguments with results, but the array doesn't seem to be storing anything. I've extracted the relevant code into the following demo: #!/usr/bin/env bash #

Memoized objects still have their __init__() invoked?

爷,独闯天下 提交于 2019-12-24 20:20:48
问题 So I am creating a memoized class, and have been observing a strange behavior. Here's the snippet of the code: class SomeClass(object): _Memoized = {} def __new__(cls, id: str, *args, **kwargs): if id not in cls._Memoized: print('New Instance') cls._Memoized[id] = super(SomeClass, cls).__new__(cls, *args, **kwargs) else: print('Existing Instance') return cls._Memoized[id] def __init__(self, id: str): print('Running init') self.id = id def test(): w1 = SomeClass(id='w1') wx = SomeClass(id='w1'

Implementing a memoization function in Haskell

主宰稳场 提交于 2019-12-24 12:34:54
问题 I'm fairly new to Haskell, and I'm trying to implement a basic memoization function which uses a Data.Map to store computed values. My example is for Project Euler Problem 15, which involves computing the number of possible paths from 1 corner to the other in a 20x20 grid. This is what I have so far. I haven't tried compiling yet because I know it won't compile. I'll explain below. import qualified Data.Map as Map main = print getProblem15Value getProblem15Value :: Integer getProblem15Value =