... or the no-sentinel find-based approach:

(if-let [[_ v] (find a-map :b)]
  v
  (my-foo))

Cheers,

Dave



On Mon, Oct 28, 2013 at 9:08 AM, Alex Baranosky <
alexander.barano...@gmail.com> wrote:

> Or a shorter variant of the sentinel approach:
>
> (let [r (get a-map :b ::unfound)]
>   (if (= r ::unfound)
>     (my-foo)
>     r))
>
>
> On Sun, Oct 27, 2013 at 2:57 PM, Cedric Greevey <cgree...@gmail.com>wrote:
>
>> (get a-map :b my-foo) will result in the function object itself being
>> returned if :b is not found. If you want it to be called only in the event
>> of not found, you need either
>>
>> (if (contains? a-map :b) (a-map :b) (my-foo)) -- which may perform the
>> lookup twice -- or
>>
>> (if-let [r (a-map :b)] r (my-foo)) -- which does not, but treats a {:b
>> nil} entry as the same as absence of :b -- or
>>
>> (let [sentinel (Object.)
>>       r (get a-map :b sentinel)]
>>   (if (identical? r sentinel)
>>     (my-foo)
>>     r)) -- which is longer and more complex, but calls my-foo (and
>> returns what it returns) if and only if :b is genuinely absent from the map
>> (like the first solution above) and performs the lookup only once (like the
>> second solution above). If this has to be done in a tight loop, the extra
>> efficiency over the second solution may be worth it, but you'll want to
>> avoid repeatedly creating and discarding the sentinel object as well,
>> resulting in something like
>>
>> (def {^:private} sentinel (Object.))
>>
>> ...
>>
>> (defn ...
>> ...
>>   (loop ...
>> ...
>>     (let [r (get a-map :b sentinel)]
>>       (if (identical? r sentinel)
>>         (my-foo)
>>         r)) ... ) ... )
>>
>>
>>
>> On Sun, Oct 27, 2013 at 1:00 PM, Ryan <arekand...@gmail.com> wrote:
>>
>>> Silly me, thank you for your replies guys!
>>>
>>> One more question though, what if my-foo had parameters?
>>>
>>> Ryan
>>>
>>>
>>> On Sunday, October 27, 2013 6:55:34 PM UTC+2, Luc wrote:
>>>
>>>> You are getting my-foo evaluated, remove the parens around it.
>>>>
>>>> Luc P.
>>>>
>>>>
>>>> > Hello,
>>>> >
>>>> > I am trying to understanding why is this happening:
>>>> >
>>>> > > (defn my-foo [] (println "Why do I get printed?"))
>>>> > > #'sandbox4724/my-foo
>>>> > > > (get {:b 1} :b (my-foo))
>>>> > > Why do I get printed?
>>>> > > 1
>>>> > > >
>>>> >
>>>> >
>>>> > Shouldn't (my-foo) only be called in case the key isn't found? Why am
>>>> I
>>>> > seeing the above behavior instead?
>>>> >
>>>> > Thank you for your time,
>>>> >
>>>> > Ryan
>>>> >
>>>> > --
>>>> > --
>>>> > You received this message because you are subscribed to the Google
>>>> > Groups "Clojure" group.
>>>> > To post to this group, send email to clo...@googlegroups.com
>>>> > Note that posts from new members are moderated - please be patient
>>>> with your first post.
>>>> > To unsubscribe from this group, send email to
>>>> > clojure+u...@**googlegroups.com
>>>> > For more options, visit this group at
>>>> > http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en>
>>>> > ---
>>>> > You received this message because you are subscribed to the Google
>>>> Groups "Clojure" group.
>>>> > To unsubscribe from this group and stop receiving emails from it,
>>>> send an email to clojure+u...@**googlegroups.com.
>>>> > For more options, visit 
>>>> > https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>.
>>>>
>>>> >
>>>> --
>>>> Softaddicts<lprefo...@**softaddicts.ca> sent by ibisMail from my ipad!
>>>>
>>>  --
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clojure@googlegroups.com
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> clojure+unsubscr...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/clojure?hl=en
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to clojure+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>>  --
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clojure@googlegroups.com
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>  --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to