Re: Using an atom for a caching map

2014-08-30 Thread Eldar Gabdullin
Something like the following would be fine for me (def cache (atom {})) (defn lookup [cache k] (let [v (get @cache k ::nil)] (if (= v ::nil) (let [v (calc-value k)] (swap! cache assoc k v) v) v))) (let [value (lookup cache k)] ; use value and @cache here

Re: Using an atom for a caching map

2014-08-30 Thread Colin Fleming
True, but only if you don't mind possibly calculating the value more than once since the update is not atomic. On 30 August 2014 18:31, Eldar Gabdullin eldar...@gmail.com wrote: Something like the following would be fine for me (def cache (atom {})) (defn lookup [cache k] (let [v (get

Re: Using an atom for a caching map

2014-08-30 Thread Eldar Gabdullin
Snippet you showed before is also not an atomic. If you want strictly to avoid recomputations you need something more elaborate. May be this (def cache (atom {})) (defn unwrap [v] (if-let [a (get v ::atom)] @a v)) (defn compute [cache k] (let [p {::atom (atom nil)} c

Re: Using an atom for a caching map

2014-08-30 Thread Ray Miller
On 30 August 2014 06:26, Colin Fleming colin.mailingl...@gmail.com wrote: I want to use a map to cache values based on a key. I'm planning to use an atom for this. My basic operation is give me the value for this key - if the value exists in the map then that value should be returned,

Re: Using an atom for a caching map

2014-08-30 Thread Eldar Gabdullin
Oh, forget to wait for value in unwrap. Here is a better version (defn unwrap [v] (if-let [p (get v ::promise)] @p v)) (defn compute [cache k] (let [p {::promise (promise)} c (swap! cache assoc k p) val (get c k)] (when (identical? val p) (let [result

Re: How can I add meta to an object that doesn't implement IObj?

2014-08-30 Thread François Rey
On 30/08/14 05:15, Atamert Ölçgen wrote: Obviously I can't. But I need to add this capability to an object. During testing I attach meta to this object that contains an atom. Then I pass this object to other functions, known in runtime. I can't use a dynamic var because all this happens

Re: How can I add meta to an object that doesn't implement IObj?

2014-08-30 Thread Atamert Ölçgen
Hi François, Thanks for the links. I can't really pass a list since the first function is expecting the object I am passing, then it calls some other functions that were supposed to extract the meta. The problem is, functions that are called later can be executed in a different thread. On

Re: How can I add meta to an object that doesn't implement IObj?

2014-08-30 Thread Alex Baranosky
The only way to do this is to wrap the object in something that implements IObj. But at that point you might as well wrap it in a regular hashmap and just wrap it in real data. On Sat, Aug 30, 2014 at 1:54 AM, Atamert Ölçgen mu...@muhuk.com wrote: Hi François, Thanks for the links. I can't

Re: Using an atom for a caching map

2014-08-30 Thread Colin Fleming
Yes, in my case the update is atomic but doesn't strictly avoid recomputation (since the Atom can call the calculation function many times). I've given this some hammock time and I don't think that's a problem. However on further investigation I do have to ensure that for each of my keys the value

Re: Using an atom for a caching map

2014-08-30 Thread Colin Fleming
In my case I can't use memoize because I need to supply the cache map - that in turn is stored on another object so it can be invalidated by events outside my control. On 30 August 2014 20:00, Ray Miller r...@1729.org.uk wrote: On 30 August 2014 06:26, Colin Fleming colin.mailingl...@gmail.com

Re: Using an atom for a caching map

2014-08-30 Thread Eldar Gabdullin
I see... Just in case, I'd like to point out one possible deficiency in your original snippet. Generally, it should be better move value calculation outside of swap call, thus reducing a chance of contention, especially when you have many threads asking for different keys. суббота, 30

Re: How can I add meta to an object that doesn't implement IObj?

2014-08-30 Thread Francis Avila
It would probably help if you said more about the source of this atom-holding object. Is it a plain Java class? A deftype/defrecord? Is it final? If you can control the construction of this object and its class is not final, you can subclass it and add an IObj implementation. (Note that most,

Re: Why is this an Exception ?

2014-08-30 Thread Sreeharsha Mudivarti
On Saturday, August 30, 2014 12:34:24 AM UTC+5:30, Ashton Kemerling wrote: Bar/baz refers to the symbol baz in the namespace bar. If you haven't created and imported bar, that's an error. This feels too java-ish for a lisp. Can I avoid this by some form of declaration ? -- You received

Re: Using an atom for a caching map

2014-08-30 Thread Colin Fleming
I'm not sure I understand - the contention will be the same whether or not the value is calculated in the swap! call, right? And if I calculate the value and then pass the calculated value to swap!, that value will be thrown away every time the cache contains an existing value for my key. On 30

Re: Using an atom for a caching map

2014-08-30 Thread Eldar Gabdullin
You are absolutely right. Sorry, for distraction. Deleted my message immediately after I realized that, but it turns out it managed to go already. суббота, 30 августа 2014 г., 16:25:21 UTC+4 пользователь Colin Fleming написал: I'm not sure I understand - the contention will be the same

Re: How can I add meta to an object that doesn't implement IObj?

2014-08-30 Thread Gary Trakhman
Can you monkey-patch the meta function itself to draw from a map of object to meta? On Saturday, August 30, 2014, Francis Avila franci...@gmail.com wrote: It would probably help if you said more about the source of this atom-holding object. Is it a plain Java class? A deftype/defrecord? Is it

Puzzling list comp error: Don't know how to create ISeq from: clojure.lang.Keyword

2014-08-30 Thread gvim
I have a long function which produces `list-of-lists` : ((Sun 21 li 13 201.2139410) (Moon 11 le 21 131.3457459) ..) before entering a list comprehension (simplified for brevity): (defn calc ... (let [ . ... list-of-lists (map #(rest (first %))

Re: Puzzling list comp error: Don't know how to create ISeq from: clojure.lang.Keyword

2014-08-30 Thread Alexey Kachayev
for macro expects each pair to be either binding-form/collection-expr or one of known modifiers (:let, :when, :while). Here: plan (keyword (first l)) you give a pair of binding-form and keyword (which is really impossible to iterate over). If you meant let-binding for plan, dec, min and long,

Re: Puzzling list comp error: Don't know how to create ISeq from: clojure.lang.Keyword

2014-08-30 Thread gvim
On 30/08/2014 15:07, Alexey Kachayev wrote: for macro expects each pair to be either binding-form/collection-expr or one of known modifiers (:let, :when, :while). Here: plan (keyword (first l)) you give a pair of binding-form and keyword (which is really impossible to iterate over). If you

Re: Puzzling list comp error: Don't know how to create ISeq from: clojure.lang.Keyword

2014-08-30 Thread gvim
On 30/08/2014 15:07, Alexey Kachayev wrote: for macro expects each pair to be either binding-form/collection-expr or one of known modifiers (:let, :when, :while). Here: plan (keyword (first l)) you give a pair of binding-form and keyword (which is really impossible to iterate over). If you

Re: Puzzling list comp error: Don't know how to create ISeq from: clojure.lang.Keyword

2014-08-30 Thread Alexey Kachayev
Thread-first macro - will insert list-of-lists as first argument for map, which is definitely not what you expect. Use threading-last - instead. 2014-08-30 18:48 GMT+03:00 gvim gvi...@gmail.com: On 30/08/2014 15:07, Alexey Kachayev wrote: for macro expects each pair to be either

Re: Puzzling list comp error: Don't know how to create ISeq from: clojure.lang.Keyword

2014-08-30 Thread gvim
On 30/08/2014 17:04, Alexey Kachayev wrote: Thread-first macro -will insert list-of-listsas first argument for map, which is definitely not what you expect. Use threading-last -instead. I've never quite understood the distinction other than - does everything top to bottom and - does the

Re: Using an atom for a caching map

2014-08-30 Thread Beau Fabry
I must be missing something, because this is too simple? (defn get-maybe-cached [cache key] (dosync (if-let [v (get @cache key)] v (do (reset! cache (assoc @cache key (calculate key))) (get @cache key) On Saturday, August 30, 2014

Re: Using an atom for a caching map

2014-08-30 Thread Marcus Magnusson
How about something like this? You can still keep your cache as an atom that you can pass around, and you avoid unnecessary recomputation through memoize: (def cache (atom {})) (def lookup (let [calc-value* (memoize calc-value)] (fn [cache k] (when-not (contains? @cache k)

Re: Why is this an Exception ?

2014-08-30 Thread Andy Fingerhut
How would another Lisp avoid giving you an error when referring to a symbol that has no value? If you just want your entire program in a single namespace, and don't want to use any libraries in other namespaces, then simply don't use the / character in your symbol names. If you want to use the

Re: Puzzling list comp error: Don't know how to create ISeq from: clojure.lang.Keyword

2014-08-30 Thread Sam Raker
`-` inserts its first argument into the second position of the next argument, and so on, so (- [] (conj 1) (conj 2)) Turns into (conj (conj [] 1) 2) `-` inserts its first argument into the LAST position of the next argument, and so on, so (- 1 (conj [2]) (conj [3])) Turns into (conj

Re: Resources for intermediate/not-absolute-beginner Clojurians

2014-08-30 Thread Christopher Small
+1 for JOC. It's a fantastic book. Chris On Friday, August 29, 2014 6:28:25 PM UTC-7, Paul L. Snyder wrote: On Fri, 29 Aug 2014, Sam Raker wrote: I'm just not sure what to do at this point in my Clojure learning experience. I've probably written a few thousand lines of Clojure at

Re: Resources for intermediate/not-absolute-beginner Clojurians

2014-08-30 Thread Gary Trakhman
Eventually you'll have to come around to reading implementations. The Joy of Clojure is a very thoughtful book, but lib authors aren't going to go through the same amount of trouble. Reading code gets easier and rewards faster the more you do it. On Saturday, August 30, 2014, Christopher Small

Re: Resources for intermediate/not-absolute-beginner Clojurians

2014-08-30 Thread Alex Miller
Hi Sam, I am working on a book for Pragmatic Programmers with Ben Vandgrift called Clojure Applied that is target specifically at people like yourself. Our goal is to bridge the gap between knowing the syntax and basics of the language to knowing how to apply it in building applications. The