<?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>memoize -- record results of function evaluation for future use</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_merge__Pairs.html">next</a> | <a href="_member.html">previous</a> | <a href="_merge__Pairs.html">forward</a> | <a href="_member.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>memoize -- record results of function evaluation for future use</h1> <div class="single"><h2>Description</h2> <div><tt>memoize f</tt> -- produces, from a function <tt>f</tt>, a new function that behaves the same as <tt>f</tt>, but remembers previous answers to be provided the next time the same arguments are presented.<p/> <table class="examples"><tr><td><pre>i1 : fib = n -> if n <= 1 then 1 else fib(n-1) + fib(n-2) o1 = fib o1 : FunctionClosure</pre> </td></tr> <tr><td><pre>i2 : time fib 16 -- used 0.001999 seconds o2 = 1597</pre> </td></tr> <tr><td><pre>i3 : fib = memoize fib --warning: function fib redefined o3 = fib o3 : FunctionClosure</pre> </td></tr> <tr><td><pre>i4 : time fib 16 -- used 0. seconds o4 = 1597</pre> </td></tr> <tr><td><pre>i5 : time fib 16 -- used 0. seconds o5 = 1597</pre> </td></tr> </table> <p/> The function <tt>memoize</tt> operates by constructing a <a href="___Mutable__Hash__Table.html" title="the class of all mutable hash tables">MutableHashTable</a> in which the argument sequences are used as keys for accessing the return value of the function.<p/> An optional second argument to memoize provides a list of initial values, each of the form <tt>x => v</tt>, where <tt>v</tt> is the value to be provided for the argument <tt>x</tt>.<p/> Warning: when the value returned by <tt>f</tt> is <a href="_null.html" title="the unique member of the empty class">null</a>, it will always be recomputed, even if the same arguments are presented.<p/> Warning: the new function created by <tt>memoize</tt> will save references to all arguments and values it encounters, and this will often prevent those arguments and values from being garbage-collected as soon as they might have been. If the arguments are implemented as mutable hash tables (modules, matrices and rings are implemented this way) then a viable strategy is to stash computed results in the arguments themselves. See also <tt>CacheTable</tt>.</div> </div> <div class="waystouse"><h2>Ways to use <tt>memoize</tt> :</h2> <ul><li>memoize(Function)</li> <li>memoize(Function,List)</li> </ul> </div> </div> </body> </html>