<?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: Resettable Memoize in Clojure</title>
	<atom:link href="http://www.paullegato.com/blog/memoize-reset-clojure/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paullegato.com/blog/memoize-reset-clojure/</link>
	<description></description>
	<lastBuildDate>Thu, 12 Aug 2010 21:24:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Paul Legato</title>
		<link>http://www.paullegato.com/blog/memoize-reset-clojure/comment-page-1/#comment-21</link>
		<dc:creator>Paul Legato</dc:creator>
		<pubDate>Sun, 28 Feb 2010 00:30:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.paullegato.com/?p=104#comment-21</guid>
		<description>Very nice. Thanks!</description>
		<content:encoded><![CDATA[<p>Very nice. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Powers</title>
		<link>http://www.paullegato.com/blog/memoize-reset-clojure/comment-page-1/#comment-19</link>
		<dc:creator>Jeff Powers</dc:creator>
		<pubDate>Fri, 26 Feb 2010 20:11:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.paullegato.com/?p=104#comment-19</guid>
		<description>I don&#039;t remember what exactly I needed this for, but here&#039;s a version of memoize that uses soft references so it will (presuming a non-braindead JVM) drop the least-recently-referenced cached results before OOMing.

[code]
(defn soft-memoize
  &quot;Returns a memoized version of a referentially transparent function. The
  memoized version of the function keeps a cache of the mapping from arguments
  to results and, when calls with the same arguments are repeated often, has
  higher performance at the expense of higher memory use.  This version uses
  SoftReferences so that the memoization will never cause an OOM.&quot;
  [f]
  (let [mem (ReferenceMap. ReferenceMap/SOFT ReferenceMap/SOFT)
        null-marker (gensym)]
    (fn [&amp; args]
      (if-let [e (.get mem args)]
        (if (= e null-marker)
          nil
          e)
        (let [ret (apply f args)]
          (.put mem args (if ret ret null-marker))
          ret)))))
[/code]</description>
		<content:encoded><![CDATA[<p>I don&#8217;t remember what exactly I needed this for, but here&#8217;s a version of memoize that uses soft references so it will (presuming a non-braindead JVM) drop the least-recently-referenced cached results before OOMing.</p>
<pre class="brush: plain;">
(defn soft-memoize
  &quot;Returns a memoized version of a referentially transparent function. The
  memoized version of the function keeps a cache of the mapping from arguments
  to results and, when calls with the same arguments are repeated often, has
  higher performance at the expense of higher memory use.  This version uses
  SoftReferences so that the memoization will never cause an OOM.&quot;
  [f]
  (let [mem (ReferenceMap. ReferenceMap/SOFT ReferenceMap/SOFT)
        null-marker (gensym)]
    (fn [&amp;amp; args]
      (if-let [e (.get mem args)]
        (if (= e null-marker)
          nil
          e)
        (let [ret (apply f args)]
          (.put mem args (if ret ret null-marker))
          ret)))))
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
