next | previous | forward | backward | up | top | index | toc | Macaulay2 website
Macaulay2Doc :: cacheValue

cacheValue -- cache values of functions in their arguments

Synopsis

Description

i1 : x = new HashTable from { val => 1000, cache => new CacheTable }

o1 = HashTable{cache => CacheTable{}}
               val => 1000

o1 : HashTable
i2 : f = (t -> (print "hi there"; t.val^4))

o2 = f

o2 : FunctionClosure
i3 : h = (cacheValue VALUE) f

o3 = -*a cache function*-

o3 : CacheFunction
i4 : h x
hi there

o4 = 1000000000000
i5 : h x

o5 = 1000000000000
i6 : peek'_2 x

o6 = HashTable{cache => CacheTable{VALUE => 1000000000000}}
               val => 1000

Code

../../../../../../../Macaulay2/m2/methods.m2:592:18-601:26: --source code:
cacheValue = key -> f -> new CacheFunction from (x -> (
          c := try x.cache else x.cache = new CacheTable;
          if c#?key then (
               val := c#key;
               if class val === CacheFunction then (
                    remove(c,key);
                    c#key = val x)
               else val
               )
          else c#key = f x))

See also

For the programmer

The object cacheValue is a function closure.