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
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
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
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
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
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
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
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-or-exc [map key]
(if-let [[_ v] (find map
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
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
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
11 matches
Mail list logo