memoization

Can I memoize a Generic Method?

a 夏天 提交于 2019-12-03 20:59:17
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>(); return a => cache.GetOrAdd(a, f); } 来源: https://stackoverflow.com/questions/56269443/can-i-memoize-a

How do I memoize a recursive function in Lisp?

孤人 提交于 2019-12-03 19:46:17
问题 I'm a Lisp beginner. I'm trying to memoize a recursive function for calculating the number of terms in a Collatz sequence (for problem 14 in Project Euler). My code as of yet is: (defun collatz-steps (n) (if (= 1 n) 0 (if (evenp n) (1+ (collatz-steps (/ n 2))) (1+ (collatz-steps (1+ (* 3 n))))))) (defun p14 () (defvar m-collatz-steps (memoize #'collatz-steps)) (let ((maxsteps (funcall m-collatz-steps 2)) (n 2) (steps)) (loop for i from 1 to 1000000 do (setq steps (funcall m-collatz-steps i))

Haskell and memoization of pure function results [duplicate]

青春壹個敷衍的年華 提交于 2019-12-03 19:04:58
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: When is memoization automatic in GHC Haskell? As a consequence, pure function always returns the same value for a fixed input. That said, does Haskell (more precisely GHC) automatically cache (memoize) these results if there is enough memory (question 1) and does developer have any control on it (question 2)? 回答1: I voted to close, but short answer: GHC does not do any automatic memoization of functions, and

Persistent memoization in Python

做~自己de王妃 提交于 2019-12-03 17:15:38
问题 I have an expensive function that takes and returns a small amount of data (a few integers and floats). I have already memoized this function, but I would like to make the memo persistent. There are already a couple of threads relating to this, but I'm unsure about potential issues with some of the suggested approaches, and I have some fairly specific requirements: I will definitely use the function from multiple threads and processes simultaneously (both using multiprocessing and from

How underscore memoize is implemented in javascript

三世轮回 提交于 2019-12-03 16:25:27
I'm developing my own functional-programming library, and now referring the underscore . memoize _.memoize(function, [hashFunction]) Memoizes a given function by caching the computed result. Useful for speeding up slow-running computations. If passed an optional hashFunction, it will be used to compute the hash key for storing the result, based on the arguments to the original function. The default hashFunction just uses the first argument to the memoized function as the key. var fibonacci = _.memoize(function(n) { return n < 2 ? n: fibonacci(n - 1) + fibonacci(n - 2); }); The above code that

A thread-safe memoize decorator

早过忘川 提交于 2019-12-03 16:19:40
I'm trying to make a memoize decorator that works with multiple threads. I understood that I need to use the cache as a shared object between the threads, and acquire/lock the shared object. I'm of course launching the threads: for i in range(5): thread = threading.Thread(target=self.worker, args=(self.call_queue,)) thread.daemon = True thread.start() where worker is: def worker(self, call): func, args, kwargs = call.get() self.returns.put(func(*args, **kwargs)) call.task_done() The problem starts, of course, when I'm sending a function decorated with a memo function (like this ) to many

Using rails presenters - memoizable getting deprecated in 3.1 - use ||= instead?

烈酒焚心 提交于 2019-12-03 14:34:35
Issue: To avoid creating multiple objects or multiple queries when possible. I am using Presenters with rails as a Best Practice. I am following advice that says that it would be good to use "extend ActiveSupport.Memoizable" (and then memoize :method(s) to use them) over setting up items with @the_record = record ||= @record style because of a couple of issues - false or nil not getting stored so the query gets called again and also that memoizable uses the cache better (i.e. uses it!). However I see that memoizable is getting deprecated in rails 3.1 Notes i github under carrierwave and with

Invalidate/prevent memoize with plone.memoize.ram

不羁的心 提交于 2019-12-03 14:27:31
I've and Zope utility with a method that perform network processes. As the result of the is valid for a while, I'm using plone.memoize.ram to cache the result. MyClass(object): @cache(cache_key) def do_auth(self, adapter, data): # performing expensive network process here ...and the cache function: def cache_key(method, utility, data): return time() // 60 * 60)) But I want to prevent the memoization to take place when the do_auth call returns empty results (or raise network errors). Looking at the plone.memoize code it seems I need to raise ram.DontCache() exception, but before doing this I

Memoizing SQL queries

帅比萌擦擦* 提交于 2019-12-03 13:36:02
Say I have a function that runs a SQL query and returns a dataframe: import pandas.io.sql as psql import sqlalchemy query_string = "select a from table;" def run_my_query(my_query): # username, host, port and database are hard-coded here engine = sqlalchemy.create_engine('postgresql://{username}@{host}:{port}/{database}'.format(username=username, host=host, port=port, database=database)) df = psql.read_sql(my_query, engine) return df # Run the query (this is what I want to memoize) df = run_my_query(my_query) I would like to: Be able to memoize my query above with one cache entry per value of

Is there an automatic way to memoise global polymorphic values in Haskell?

早过忘川 提交于 2019-12-03 12:02:37
Polymorphic "constants", like 5 :: Num a => a , aren't really constants but functions of a dictionary argument. Hence, if you define primes :: Num n => [n] primes = ... Bad example of course, there's no good reason here to have it polymorphic... what I'm really interested is if you try to globally memoise a nontrivial polymorphic function, with e.g. memo-trie s. then this sequence won't be shared between calls from different sites, which isn't nice in terms of performance. (Isn't this the main reason the Haskell standard blessed us with the Dreaded Monomorphism Restriction?) The only way I can