<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Use nested functions to memoize costly functions</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Mon, 13 Feb 2012 13:24:10 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren Shure</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-32797</link>
		<dc:creator>Loren Shure</dc:creator>
		<pubDate>Tue, 06 Dec 2011 10:37:03 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-32797</guid>
		<description>Rafael-

Clearing every reference to a particular handle should remove the memory associated with that instance.  As long as there are no other copies of the reference that f is pointing to (i.e., you didn&#039;t say g = f...), then clear f should remove the nested workspace associated with f.  In other words, as long as there are no outstanding references, the memory should be returned.

--Loren</description>
		<content:encoded><![CDATA[<p>Rafael-</p>
<p>Clearing every reference to a particular handle should remove the memory associated with that instance.  As long as there are no other copies of the reference that f is pointing to (i.e., you didn&#8217;t say g = f&#8230;), then clear f should remove the nested workspace associated with f.  In other words, as long as there are no outstanding references, the memory should be returned.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rafael</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-32796</link>
		<dc:creator>Rafael</dc:creator>
		<pubDate>Tue, 06 Dec 2011 03:02:46 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-32796</guid>
		<description>Hi Loren.
I have been converting a lot of our instrument control and data acquisition code to use nested functions and &quot;memoize&quot;.  I like it because it allows us to think of the code as if it was object-oriented, and has made it a lot more re-usable.

But I have been worrying lately about memory usage when I have lots of these function handles associated with persistent workspaces.  How can I make sure that those workspaces disappear from memory when we don&#039;t need them anymore?  In your example, if I have f = memoize(@sin) and I use it for a while, can I clear the persistent workspace by just saying &quot;clear f&quot;?  Of is there anything else I need to do?

Thanks!</description>
		<content:encoded><![CDATA[<p>Hi Loren.<br />
I have been converting a lot of our instrument control and data acquisition code to use nested functions and &#8220;memoize&#8221;.  I like it because it allows us to think of the code as if it was object-oriented, and has made it a lot more re-usable.</p>
<p>But I have been worrying lately about memory usage when I have lots of these function handles associated with persistent workspaces.  How can I make sure that those workspaces disappear from memory when we don&#8217;t need them anymore?  In your example, if I have f = memoize(@sin) and I use it for a while, can I clear the persistent workspace by just saying &#8220;clear f&#8221;?  Of is there anything else I need to do?</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-32545</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 12 Oct 2011 16:03:53 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-32545</guid>
		<description>David-

Thanks for the contribution!

--Loren</description>
		<content:encoded><![CDATA[<p>David-</p>
<p>Thanks for the contribution!</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Young</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-32544</link>
		<dc:creator>David Young</dc:creator>
		<pubDate>Wed, 12 Oct 2011 14:44:12 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-32544</guid>
		<description>Although this blog entry has been around for a while, I only read it recently. I thought a version for functions with multiple arguments and multiple results might be useful, and I&#039;ve put one in the file exchange as part of this contribution: http://www.mathworks.co.uk/matlabcentral/fileexchange/33068 . It follows your pattern quite closely, though the implementation ultimately uses containers.Map rather than == and find.</description>
		<content:encoded><![CDATA[<p>Although this blog entry has been around for a while, I only read it recently. I thought a version for functions with multiple arguments and multiple results might be useful, and I&#8217;ve put one in the file exchange as part of this contribution: <a href="http://www.mathworks.co.uk/matlabcentral/fileexchange/33068" rel="nofollow">http://www.mathworks.co.uk/matlabcentral/fileexchange/33068</a> . It follows your pattern quite closely, though the implementation ultimately uses containers.Map rather than == and find.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ljubomir Josifovski</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-31821</link>
		<dc:creator>Ljubomir Josifovski</dc:creator>
		<pubDate>Thu, 04 Nov 2010 08:30:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-31821</guid>
		<description>Just to report back: the (half-?)solution (for avoiding arguments evaluation when print is skipped) I settled for is to use logical ops short circuiting directly and turn the print calls into
&lt;pre&gt;
   is_verbose_level(...) &amp;&amp; fprintf(...);
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
Bit ugly but does it. Thanks, Ljubomir</description>
		<content:encoded><![CDATA[<p>Just to report back: the (half-?)solution (for avoiding arguments evaluation when print is skipped) I settled for is to use logical ops short circuiting directly and turn the print calls into</p>
<pre>
   is_verbose_level(...) &amp;&amp; fprintf(...);
</pre>
<pre></pre>
<p>Bit ugly but does it. Thanks, Ljubomir</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ljubomir Josifovski</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-31762</link>
		<dc:creator>Ljubomir Josifovski</dc:creator>
		<pubDate>Fri, 15 Oct 2010 16:23:53 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-31762</guid>
		<description>Many thanks for your help Loren. Yep, will try to extend your idea. Matlab would not have a general &quot;reference of any expression&quot; operator, right? By this I mean something akin to what a function handle is to a function, but aplicable to any expression (not just functions). Thanks again, Ljubomir</description>
		<content:encoded><![CDATA[<p>Many thanks for your help Loren. Yep, will try to extend your idea. Matlab would not have a general &#8220;reference of any expression&#8221; operator, right? By this I mean something akin to what a function handle is to a function, but aplicable to any expression (not just functions). Thanks again, Ljubomir</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-31760</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 15 Oct 2010 12:26:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-31760</guid>
		<description>Ljubomir-
I&#039;ve got something to work, but I am sure you will find it too specific.  Perhaps you can work with the idea, though.

&lt;pre class=&quot;code&quot;&gt;
pv = @(v, varargin) (v&lt;=1) &amp;&amp; fprintf(varargin{1},varargin{2}());
slow = @() fprintf(&#039;slow called ...\n&#039;);
pv(1, &#039;   when needed\n&#039;, slow);
pv(2, &#039;   even when not needed\n&#039;, slow);
&lt;/pre&gt;

&lt;pre class=&quot;code&quot;&gt;
&gt;&gt; pv(1, &#039;   when needed\n&#039;, slow);
slow called ...
   when needed
&gt;&gt; pv(2, &#039;   even when not needed\n&#039;, slow);
&gt;&gt; 
&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Ljubomir-<br />
I&#8217;ve got something to work, but I am sure you will find it too specific.  Perhaps you can work with the idea, though.</p>
<pre class="code">
pv = @(v, varargin) (v< =1) &#038;&#038; fprintf(varargin{1},varargin{2}());
slow = @() fprintf('slow called ...\n');
pv(1, '   when needed\n', slow);
pv(2, '   even when not needed\n', slow);
</pre>
</pre><pre class="code">
>> pv(1, '   when needed\n', slow);
slow called ...
   when needed
>> pv(2, '   even when not needed\n', slow);
>>
</pre>
<p>--Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-31759</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 15 Oct 2010 11:59:36 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-31759</guid>
		<description>Ljubomir-

slow() is getting called each timein yours because the arguments to pv get evaluated before making the call to pv.  You need to pass in slow, and not slow().  And you need to rewrite pv to execute slow when required.  You are always causing slow to be executed right now.

--Loren</description>
		<content:encoded><![CDATA[<p>Ljubomir-</p>
<p>slow() is getting called each timein yours because the arguments to pv get evaluated before making the call to pv.  You need to pass in slow, and not slow().  And you need to rewrite pv to execute slow when required.  You are always causing slow to be executed right now.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-31758</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 15 Oct 2010 11:35:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-31758</guid>
		<description>Ljubomir-

I can&#039;t think of a good way around it right now without explicitly writing a function instead of using an anonymous function.  Note that your second pv statement itself doesn&#039;t print, though slow executes.  Do you have an example that is more numerically oriented?  I bet the logic would be easier (for me) to trace then.

You might ask on the newsgroup.  Someone there may know how to do this off the top of their heads.

--Loren</description>
		<content:encoded><![CDATA[<p>Ljubomir-</p>
<p>I can&#8217;t think of a good way around it right now without explicitly writing a function instead of using an anonymous function.  Note that your second pv statement itself doesn&#8217;t print, though slow executes.  Do you have an example that is more numerically oriented?  I bet the logic would be easier (for me) to trace then.</p>
<p>You might ask on the newsgroup.  Someone there may know how to do this off the top of their heads.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ljubomir Josifovski</title>
		<link>http://blogs.mathworks.com/loren/2006/02/08/use-nested-functions-to-memoize-costly-functions/#comment-31756</link>
		<dc:creator>Ljubomir Josifovski</dc:creator>
		<pubDate>Fri, 15 Oct 2010 09:30:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=19#comment-31756</guid>
		<description>Thanks Loren. Do you maybe know of a way of applying this conveniently to varargin-s? For example in
&lt;pre&gt;
  pv = @(v, varargin) (v&lt;=1) &amp;&amp; fprintf(varargin{:});
  slow = @() fprintf(&#039;slow called ...\n&#039;);
  pv(1, &#039;   when needed\n&#039;, slow());
  pv(2, &#039;   even when not needed\n&#039;, slow());
&lt;/pre&gt;
would like to avoid the call to slow() in the course of the 2nd call to pv(), pv(2, ...). Thanks, Ljubomir</description>
		<content:encoded><![CDATA[<p>Thanks Loren. Do you maybe know of a way of applying this conveniently to varargin-s? For example in</p>
<pre>
  pv = @(v, varargin) (v&lt;=1) &amp;&amp; fprintf(varargin{:});
  slow = @() fprintf('slow called ...\n');
  pv(1, '   when needed\n', slow());
  pv(2, '   even when not needed\n', slow());
</pre>
<p>would like to avoid the call to slow() in the course of the 2nd call to pv(), pv(2, &#8230;). Thanks, Ljubomir</p>
]]></content:encoded>
	</item>
</channel>
</rss>

