3 ms·
"Wouldn’t it be cool if we could just write the recursive function, and then have some generic machinery make it fast for us by automatically generating a memo
by thesuperbigfrog 3y ago
"Wouldn’t it be cool if we could just write the recursive function, and then have some generic machinery make it fast for us by automatically generating a memo table?"
"In other words, we’d like a magic memoization function . . . Then we could just define our slow, recursive function normally, wave our magic memo wand over it, and get a fast version for free!"
Perl has this exact functionality with Memoize:
https://perldoc.perl.org/Memoize https://perldoc.perl.org/Memoize
More programming languages should have this functionality built in.
- mrfox321 3y agosame with python: @functools.cache def fn(x): https://docs.python.org/3/library/functools.html#functools.cache https://docs.python.org/3/library/functools.html#functools.c...
- deleted 3y ago[deleted]
- dndn1 3y agoI develop calculang, a language for calculations. It converts concise formulae into Javascript. Since calculation formulae are usually inherently pure, a `--memo` flag to the compiler memoizes the lot. Many of my models are written in this recursive style and only work because of memoization. Sometimes other tricks are needed e.g. careful ordering of the calls, so that the memo is available before the stack breaks. My first calculang example is a bouncing ball: the position at t depends on speed and the position at previous t. With loops we'll usually discover the same thing, but it can be so much harder to follow. First-class memoization to proliferate this style is a really neat thing. [0] https://github.com/calculang/calculang https://github.com/calculang/calculang [1] bouncing ball code: https://github.com/declann/calculang-miscellaneous-models/blob/main/models/bounce/bounce.cul.js https://github.com/declann/calculang-miscellaneous-models/bl... [2] bouncing ball initial post: https://observablehq.com/@declann/calculang-bouncing-ball?collection=@declann/calculang https://observablehq.com/@declann/calculang-bouncing-ball?co... [3] if you can tolerate my slow-load WIP devtools and are on a Desktop, this is better, with buttons to navigate through the model development (only some of which are working now!): https://models-on-a-plane.pages.dev/stories/bounce/ https://models-on-a-plane.pages.dev/stories/bounce/