memoizing functions that throw exceptions

Description

core.memoize doesn't retry if the orginal function throws an exception.
This is different to clojure.core/memoize, and looks like it's a result of using delays in core.memoize.
I think the core/memoize behaviour makes more sense - if an exception is thrown do not memoize the result, instead retry the function.
Example below.


(ns test-memo.core
(:require [clojure.core.memoize ])
(:import [java.lang.RuntimeException]))

(defn print-and-throw []
(do (println "hi") (throw (RuntimeException.))))

(def normal (memoize print-and-throw))
(def lru (clojure.core.memoize/lru print-and-throw))

user> (test-memo.core/normal)
hi
RuntimeException test-memo.core/print-and-throw (core.clj:6)
user> (test-memo.core/normal)
hi
RuntimeException test-memo.core/print-and-throw (core.clj:6)

(test-memo.core/lru)
hi
RuntimeException test-memo.core/print-and-throw (core.clj:6)
user> (test-memo.core/lru)
NullPointerException clojure.core.memoize/through*/fn--2771/fn--2772 (memoize.clj:53)

Environment

core.memoize 0.5.6
clojure 1.5.1

Attachments

1
  • 10 Oct 2015, 12:18 AM

Activity

Show:

Alex Miller 
November 6, 2015 at 4:58 PM

A version of this was implemented in core.memoize 0.5.7 with d-lay. There is a race condition in that implementation that is addressed in CMEMOIZE-21.

Leon Barrett 
November 2, 2015 at 11:28 PM

I've moved this race condition patch to a separate task: http://dev.clojure.org/jira/browse/CMEMOIZE-21

Leon Barrett 
October 16, 2015 at 7:05 PM

Hi. It's been a week. Can someone take a look at my patch? It would fix a longstanding issue.

Thanks.

Leon Barrett 
October 10, 2015 at 12:20 AM

I've submitted a patch to fix the last remaining little bit, the unsynchronized deref that causes the race condition. What else needs to be done for this to be merged into master?

Thanks.

Leon Barrett 
October 10, 2015 at 12:18 AM

Here is a patch to fix the race condition as recommended by Jozef Wagner.

Completed

Details

Assignee

Reporter

Priority

Created July 9, 2013 at 12:41 PM
Updated November 6, 2015 at 4:58 PM
Resolved November 6, 2015 at 4:58 PM
Loading...