Reputation
Next tag badge:
211/100 score
19/20 answers
Badges
6 204 295
Newest
 Nice Answer
Impact
~448k people reached

Jun
16
comment When does Mathematica automatically use dispatch tables for function evaluation?
In my experience, even millions of simple defintions didn't significantly slow down function dispatch mechanism. You may see something like a 30% slowdown if you move from say 100000 definitions to say 5000000 definitions. As far as I know, definitions not using patterns, are stored internally in a hash table. Definitions using patterns are stored differently, also using certain optimizations, AFAIK. Those optimizations might not scale as well with the number of definitions. Can you provide a self-contained example with working code? It is hard to tell from your description, what's going on.
Jun
14
awarded  Announcer
Jun
13
comment Cross referencing symbols in different packages
Good to know it helped. Re: accept - you don't have to do anything. If you want, you can become one of the closers for this question, by adding your vote to close this one as a duplicate (if you have the right to close questions, I don't remember how much rep it takes for that). If you feel like it, you can vote for that discussion question / answer. One thing though: please don't delete this one - it will serve as another gateway to that discussion.
Jun
13
revised How to properly handle mutual imports of multiple packages?
Replaced with a better version
Jun
13
comment Cross referencing symbols in different packages
Actually, I just realized that this was asked already before. I will port this version of my answer to that post, which contains a less extensive one.
Jun
13
comment Memoization with pure functions?
@OleksandrR. The keys are stored by value, but this is a little tricker than just that: you can do, e.g. ClearAll[a]; assoc = <|a->1|>; a=1; assoc, to dicsover that assoc still returns <| a -> 1|> - so, the keys are computed at the time when they are added to an assoc, but then later they don't change even when / if they are changed outside. Of course, in this example, you then can't really get the value any more either by assoc[a] or by assoc[1] - you would need assoc[Unevaluated@a].
Jun
13
comment Memoization with pure functions?
@OleksandrR. I am not aware of plans to change the garbage collector in a near future, but that doesn't mean there arent - I may well not be aware of them (will ask around when I get a chance). FWIW, Associations are already very widely used internally, to the extent that it's kind of hard to imagine how we managed to live without them for so long. But they are mostly used (as far as I could see) as more high-level constructs, while my use above is pretty hard-core, so I fully share your concerns.
Jun
13
comment Memoization with pure functions?
@OleksandrR. One thing I forgot to mention here is that Association is ordered, so that the order in which key-value pairs were added is preserved (which is another difference bewteen it and DownValues). This is why I can be sure that the newly added key-value pairs will be always on the right, and the older ones on the left.
Jun
13
comment Memoization with pure functions?
@OleksandrR. ... insert / delete complexity, do not produce new immutable structures (are mutable), so require manual memory management (are not automatically garbage-collected), and also, would for many applications require deep-copying of all key-value pairs, which would indeed be O(N) operation. Re: suboptimal strategy - yes, I actually noted that in one paragraph at the end of that section. Was too lazy to add an implementation for that, but that's pretty straightforward. Besides, this would still be a tradeoff, since other strategies would slow down the lookup of cached values.
Jun
13
comment Memoization with pure functions?
@OleksandrR. Thanks, I appreciate! Re: Assocition insert complexity - should be O(log N) with a fairly small constant, so effectively constant time up to very large N. Association is not a usual hash table - it is a persistent data structure. I wrote a bit more about it here and here, but the main idea is that while addition of new /removal of old key-value pairs is cheap, you still get a brand new copy of an original association. It is better than DownValues, which, while having similar ...
Jun
12
comment Memoization with pure functions?
Well, my guess is that the OP simply didn't know how to formulate better what she wanted, and used that f as a kind of hint to herself and the readers of the question.
Jun
12
comment Memoization with pure functions?
As to the standard memoization idiom you have described here, there have been multiple discussions of the standard memoization here before, for example here, or here (section called "Memoization / caching"). So, when answering, I was assuming that this standard idiom is well-known to the OP, who actually knew about that but wanted to use pure functions (Function).
Jun
12
comment Memoization with pure functions?
Well, the main thing that I found interesting about a method I proposed is that it leads to automatic garbage-collection of results which are no longer needed - which is quite important in practice. The other thing which is quite hard to control in the standard approach you describe here, is a number of memoized results - in case when such control is desired, while in my suggested approach this is relatively easy.
Jun
12
comment Trying to Visualize a Collatz - The Collatz conjecture
+1. Interesting - I only saw this question and your answer now, but in my answer on memoization with pure functions I have used a pretty similar - although not quite the same - construct to store cached values (I did also use exactly the construct you used here, many times before, but the funny thing is that two rather similar approaches were published independently in a short period of time).
Jun
12
revised Memoization with pure functions?
Added a section on controlled caches
Jun
12
comment How to define a recursive pattern?
It is probably because ReplaceAll isn't HoldAll. Rojo has explained that in his post on the second argument of Return.
Jun
12
comment Memoization with pure functions?
@AlexeyPopkov I did some simple benchmarks, it looks like the traditional approach is about twice faster for code that does very little. If the computation of the function is even a little expensive, the difference will likely be negligible.
Jun
12
awarded  Good Answer
Jun
12
revised Memoization with pure functions?
Fixed a few typos, added some more explanations / conclusions and a link.
Jun
12
comment Memoization with pure functions?
@WReach Thanks. I guess I was lucky to answer this first, I am sure that otherwise you'd leave me no chance to add anything :). I added a section on terminology, to stress that point more - see if you like the new text better.