Re: Throwing an Exception with get

2011-03-21 Thread Jonathan Smith
Here is a way that should work. (let [missing (gensym)] (defn get-with-exception [map key] (let [res (get map key missing)] (if (= res missing) (throw (new Exception my-exception)) res Gensyms are unique so you also don't have the problem of 'what happens if I

Re: Throwing an Exception with get

2011-03-21 Thread Mikhail Kryshen
On Mon, 21 Mar 2011 07:52:45 -0700 (PDT) Jonathan Smith jonathansmith...@gmail.com wrote: Here is a way that should work. (let [missing (gensym)] (defn get-with-exception [map key] (let [res (get map key missing)] (if (= res missing) (throw (new Exception

Re: Throwing an Exception with get

2011-03-21 Thread Jonathan Smith
Ah, interesting. You'll notice that the gensym is created outside the defn and captured, so I'm not sure speed is important. On Mar 21, 11:26 am, Mikhail Kryshen mikh...@kryshen.net wrote: On Mon, 21 Mar 2011 07:52:45 -0700 (PDT) Jonathan Smith jonathansmith...@gmail.com wrote: Here is a way

Re: Throwing an Exception with get

2011-03-21 Thread Daniel Werner
On 21 March 2011 15:52, Jonathan Smith jonathansmith...@gmail.com wrote: Here is a way that should work. (let [missing (gensym)]  (defn get-with-exception [map key]    (let [res (get map key missing)]      (if (= res missing)          (throw (new Exception my-exception))        res

Throwing an Exception with get

2011-03-20 Thread Andreas Kostler
Hi all, I would like to throw an exception when I'm trying to retrieve a value in a map for a key that doesn't exist. The obvious first shot was: (get {:foo bar} :foo (throw (new Exception Oh no!))) However, this doesn't work because the exception always throws since get apparently eagerly

Re: Throwing an Exception with get

2011-03-20 Thread Ambrose Bonnaire-Sergeant
This is slower but is a bit clearer. (defn get-with-exception [map key] (if (contains? map key) (get map key) (throw (Exception. oops Curious, why do you want to do this? Are you working with Java interop? Ambrose On Sun, Mar 20, 2011 at 5:50 PM, Andreas Kostler

Re: Throwing an Exception with get

2011-03-20 Thread Ambrose Bonnaire-Sergeant
Maybe something along these lines: (def ^{:private true} EMPTY (reify)) (defn get-with-empty-check [map key] (get map key EMPTY)) (defn key-exists? [value] (not= EMPTY value)) = (def res (get-with-empty-check {:asdf 1} :ss)) EMPTY = (key-exists? res) false The basic idea being that it's

Re: Throwing an Exception with get

2011-03-20 Thread Daniel Werner
On Mar 20, 10:50 am, Andreas Kostler andreas.koestler.le...@gmail.com wrote: I would like to throw an exception when I'm trying to retrieve a value in a map for a key that doesn't exist. Another concise solution (thanks, Conj Labs): (defn get-o­r-exc [map key] (if-l­et [[_ v] (find­ map

Re: Throwing an Exception with get

2011-03-20 Thread Andreas Kostler
Would that be flow control though? I see this exception as a rather exceptional circumstance for this application... On 20/03/2011, at 10:53 PM, Daniel Werner wrote: On Mar 20, 10:50 am, Andreas Kostler andreas.koestler.le...@gmail.com wrote: I would like to throw an exception when I'm trying

Re: Throwing an Exception with get

2011-03-20 Thread Daniel Werner
On 20 March 2011 22:02, Andreas Kostler andreas.koestler.le...@gmail.com wrote: Would that be flow control though? I see this exception as a rather exceptional circumstance for this application... If a missing key signifies an error, then yes, it should probably throw an exception. It seems

Re: Throwing an Exception with get

2011-03-20 Thread Andreas Kostler
Well, get-with-exception attempts to model a restriction on a relation (e.g. select in sql) so I believe a missing key indeed indicates an error. On 21/03/2011, at 9:25 AM, Daniel Werner wrote: On 20 March 2011 22:02, Andreas Kostler andreas.koestler.le...@gmail.com wrote: Would that be flow