Re: Throwing an Exception with get

2011-03-21 Thread Daniel Werner
On 21 March 2011 15:52, Jonathan Smith 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 You may be able to

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 wrote: > On Mon, 21 Mar 2011 07:52:45 -0700 (PDT) > > Jonathan Smith wrote: > > Here is a way that should work. > > > (let [missing (g

Re: Throwing an Exception with get

2011-03-21 Thread Mikhail Kryshen
On Mon, 21 Mar 2011 07:52:45 -0700 (PDT) Jonathan Smith 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

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-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 > wrote: >> Would that be flow control though? I see this e

Re: Throwing an Exception with get

2011-03-20 Thread Daniel Werner
On 20 March 2011 22:02, Andreas Kostler 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 reading your code wrongly reminde

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 > wrote: >> I would like to throw an exception when I'm trying to retrieve a value in a >>

Re: Throwing an Exception with get

2011-03-20 Thread Daniel Werner
On Mar 20, 10:50 am, Andreas Kostler 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 key)]­ v (thro­w (Exce­pti

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 Andreas Kostler
Hi Ambrose, No java interop. I do a selection based on map keys. Suppose I have a set of maps: (def rel #{{:a "a" :b "b"}{:a "c" :b "d"}{:a "e" :b "f"}}) And I do a select-like query (select rel [[:a :eq "a"]]) => #{{:a "a" :b "b"}} Now, I want to distinguish between a value that doesn't match an

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 < andreas.koestl

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 e