PackageDescription: OnceUponATime


Once Upon A Time

Last published: March 24, 2007 by 'michael'

Defines 1 Classes
Extends 4 Classes


This package extends block with an API to cache the result of the block the first time through and return that in the future. The initial use case is expressions that are believed to be the same result each time, but take time to execute. The idea is to provide an API that is very much like 'value' to BlockClosure. One can send either #once or #cached to the block. We couldn't decide which was better, so you get both.

[1000 factorial] cached

Will only pay the price once for the computation. Successive evaluations of the same block will return the previously computed value. Any block that is sent #value will flush it's cached state and return the current value.

You can send #forgetCachedBlocks to any CompiledCode object and get it to flush any cached blocks. I'll see about building some tools in the browser which allow one to find them, note them, flush them.

You can also cache against a particular argument, for example:

[:arg | arg + 1000 factorial] cached: 10.

Each argument to the cache holds its own value. This is useful for example with:

imageMaker := [:extent | ... build an image ...]/
imageMaker cached: 32@32.
imageMaker cached: 24@24.
imageMaker cached: 16@16.