On Nov 4, 11:49 pm, Baishampayan Ghose <b.gh...@gmail.com> wrote:
> On Sat, Nov 5, 2011 at 11:55 AM, Martin DeMello <martindeme...@gmail.com> 
> wrote:
> > What's the cleanest way to run a piece of code if any branch of a cond
> > statement succeeded, without relying on the return value of the
> > individual clauses not to be nil?
>
> > For example, if I have the following piece of code that says I can
> > only move left or up
>
> > (cond
> >  (= dir :left) (move-left)
> >  (= dir :up)  (move-up))
>
> > but (move-left) or (move-up) could themselves return nil, is there any
> > nice way to check if one of them was called?
>
> What about something like this -
>
> (cond
>  (= dir :left) (move-left)
>  (= dir :up)  (move-up)
>  :else :nok)
>
> Or
>
> (cond
>  (= dir :left) [(move-left)]
>  (= dir :up)  [(move-up)])

Seconded. Though if you can't easily predict what move-left/move-up
might return (ie they might even return :nok) you might need to be
more careful. Use a namespaced keyword like :my-current-ns/didnt-do-
anything, or a gensym (or a fresh Object) for guaranteed uniqueness:

(let [nothing (Object.)
      cond-result (case dir :left (move-left) :up (move-up) nothing)]
  (if (identical? nothing cond-result)
    ...))

-- 
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

Reply via email to