memoization

Efficient incremental hash computation during program interpretation

那年仲夏 提交于 2020-01-04 04:05:08
问题 I'd like to write a recursively memoizing Scheme interpreter . At any point during evaluation, the interpreter should be able to detect when it receives as arguments a pair of expression and environment that it has previously seen. Plain memoization of eval and apply is inefficient . It would require looking up the arguments in a hash table on every call of eval / apply , which would require walking the entire (possibly big) arguments on hash table matches. For example, assume that the

Efficient generic Python memoize

旧城冷巷雨未停 提交于 2020-01-03 20:45:33
问题 I have a generic Python memoizer: cache = {} def memoize(f): """Memoize any function.""" def decorated(*args): key = (f, str(args)) result = cache.get(key, None) if result is None: result = f(*args) cache[key] = result return result return decorated It works, but I'm not happy with it, because sometimes it's not efficient. Recently, I used it with a function that takes lists as arguments, and apparently making keys with whole lists slowed everything down. What is the best way to do that? (i.e

Memoize for Observables in Typescript

烈酒焚心 提交于 2020-01-02 12:09:34
问题 I am looking for the best way of implementing an optimization for very expensive method that takes multiple parameters and returns an Observable. Is there an elegant way of doing it? What I am looking for is prettier version of this: class Example { constructor( private databaseService: DatabaseService, private someService: SomeService) expensive(param1: string, param2: string) : Observable<string> { if (isMemoraized(param1,param2) { return Observable.create(observer=> observer.next

Reentrant caching of “referentially transparent” IO calls

这一生的挚爱 提交于 2020-01-01 09:47:12
问题 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

Can I memoize a Generic Method?

南笙酒味 提交于 2020-01-01 06:57:08
问题 I have 2 expensive Generic methods: public T DoStuff<T>() { //return depends on T. } public T DoStuffBasedOnString<T>(string input) { //return depends on T. } Their return values can never vary for a given Type and string. Is it possible to memoize these functions? My starting point is a memoize function taken from here: https://www.aleksandar.io/post/memoization/ public static Func<T, TResult> Memoize<T, TResult>(this Func<T, TResult> f) { var cache = new ConcurrentDictionary<T, TResult>();

What can be done to speed up this memoization decorator?

爱⌒轻易说出口 提交于 2020-01-01 03:37:16
问题 What I want is a memoization decorator that: can memoize instance methods with both arguments and keyword arguments has a cache that can be cleared (globally) with one call (vs. this one that uses a per-function cache: python resettable instance method memoization decorator) is reasonably efficient I've tweaked an example I saw and came up with the following: import functools class Memoized(object): """Decorator that caches a function's return value each time it is called. If called later

Should I use recursion or memoization for an algorithm?

安稳与你 提交于 2019-12-31 13:14:52
问题 If I have a choice to use recursion or memoization to solve a problem which should I use? In other words if they are both viable solutions in that they give the correct output and can be reasonably expressed in the code I'm using, when would I use one over the other? 回答1: I pick memoization because it's usually possible to access more heap memory than stack memory. That is, if your algorithm is run on a lot of data, in most languages you'll run out of stack space recursing before you run out

In Scala, what does “extends (A => B)” on a case class mean?

僤鯓⒐⒋嵵緔 提交于 2019-12-30 04:39:05
问题 In researching how to do Memoization in Scala, I've found some code I didn't grok. I've tried to look this particular "thing" up, but don't know by what to call it; i.e. the term by which to refer to it. Additionally, it's not easy searching using a symbol, ugh! I saw the following code to do memoization in Scala here: 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)) } And it's what the case class

Ruby Conditional-Assignment and Private Methods

一世执手 提交于 2019-12-30 04:03:51
问题 From the code below, it appears the ||= operator is being evaluated from outside of the class. class Foo attr_reader :bar def baz self.bar ||= 'baz' end private attr_writer :bar end puts Foo.new.baz # => in `baz': private method `bar=' called for #<Foo:0x007fd9720829a8> (NoMethodError) Quoting from the accepted answer on Official expansion of ||= conditional assignment operator: In other words, the expansion c = c || 3 is (excluding bugs like in pre-1.9) correct. Rewriting the baz method as

In Ruby, should I use ||= or if defined? for memoization?

送分小仙女□ 提交于 2019-12-30 01:53:27
问题 Should I use if defined? return @current_user_session if defined?(@current_user_session) @current_user_session = UserSession.find Or ||= @current_user_session ||= UserSession.find I noticed the if defined? method being used more and more recently. Is there any advantage to one over the other? Personally, I prefer ||= for readability. I also think Rails might have a memoize macro which provides this behavior transparently. Is this the case? 回答1: Be careful: x ||= y assigns x = y if x returns