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