<?xml version="1.0" encoding="utf-8" ?> <!-- for emacs: -*- coding: utf-8 -*- --> <!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>cacheValue -- cache values of functions in their arguments</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_capture_lp__String_rp.html">next</a> | <a href="___Cache__Table.html">previous</a> | <a href="_capture_lp__String_rp.html">forward</a> | <a href="___Cache__Table.html">backward</a> | up | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div> </td> </tr> </table> <hr/> <div><h1>cacheValue -- cache values of functions in their arguments</h1> <div class="single"><h2>Synopsis</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>((cacheValue KEY) f) x</tt></div> </dd></dl> </div> </li> <li><div class="single">Inputs:<ul><li><span><tt>KEY</tt></span></li> <li><span><tt>f</tt>, <span>a <a href="___Function.html">function</a></span></span></li> <li><span><tt>x</tt>, an argument for <tt>f</tt> that has <span>a <a href="___Cache__Table.html">cache table</a></span> stored in it under <tt>x.cache</tt></span></li> </ul> </div> </li> <li><div class="single">Outputs:<ul><li><span><tt>f x</tt> is returned, but the value is saved in <tt>x.cache#KEY</tt> and not recomputed later. However, if the value found in <tt>x.cache#KEY</tt> is <span>an <a href="___Cache__Function.html">a cache function</a></span>, such as is returned by <tt>(stashValue KEY) f</tt>, then the value of <tt>x.cache#KEY x</tt> is returned instead, after first removing <tt>x.cache#KEY</tt> from <tt>x.cache</tt>.</span></li> </ul> </div> </li> </ul> </div> <div class="single"><h2>Description</h2> <div><table class="examples"><tr><td><pre>i1 : x = new HashTable from { val => 1000, cache => new CacheTable } o1 = HashTable{cache => CacheTable{}} val => 1000 o1 : HashTable</pre> </td></tr> <tr><td><pre>i2 : f = (t -> (print "hi there"; t.val^4)) o2 = f o2 : FunctionClosure</pre> </td></tr> <tr><td><pre>i3 : h = (cacheValue VALUE) f o3 = {*a cache function*} o3 : CacheFunction</pre> </td></tr> <tr><td><pre>i4 : h x hi there o4 = 1000000000000</pre> </td></tr> <tr><td><pre>i5 : h x o5 = 1000000000000</pre> </td></tr> <tr><td><pre>i6 : peek'_2 x o6 = HashTable{cache => CacheTable{VALUE => 1000000000000}} val => 1000</pre> </td></tr> </table> </div> </div> <div class="single"><h2>See also</h2> <ul><li><span><a href="_stash__Value.html" title="stash values of functions in their arguments">stashValue</a> -- stash values of functions in their arguments</span></li> </ul> </div> <div class="waystouse"><div class="single"><h2>Code</h2> <pre>../../m2/methods.m2:506:18-515: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))</pre> </div> </div> </div> </body> </html>