Assuming that you meant (do (move-left) (:ok)), this is insufficient except in the rare case where he doesn't care about the return value of move-left.
On Nov 5, 2:18 am, Dennis Haupt <[email protected]> wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > what about > > >> (cond (= dir :left) ((move-left)(:ok)) (= dir :up) > >> ((move-up)(:ok)) :else :nok) > > or whatever the exact syntax is > > Am 05.11.2011 08:22, schrieb Alan Malloy: > > > > > > > > > > > On Nov 4, 11:49 pm, Baishampayan Ghose <[email protected]> wrote: > >> On Sat, Nov 5, 2011 at 11:55 AM, Martin DeMello > >> <[email protected]> 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) ...)) > > - -- > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v2.0.14 (MingW32) > Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/ > > iQIcBAEBAgAGBQJOtP9TAAoJENRtux+h35aGZ6cQALNrIXvCpEUfrLFAYgauJVWP > 8cvu+gXUgCDxxcJR41MOBfDvnVOJmeiCJp0LWH3HpYGD0M+7BfJxMoyg/tdrZggn > L5CSq+oXbeOUDe/7LV7E97u8/gZhHrRNELZGyWbgHX/d0VZpezH2QwMMQKTiwwAh > zxkyUHyahJ1+Li6hrFL/1CI8XwH7JEnjgyd4Zfj2BNkkAmgS2E+iBrGYb8vMQtLo > 7Pm6WBrTRjNNyrIKi6axHij2SvKdPKSzbAR3oJPjhL+5yDhrvCdHk0AvzJjiTHsK > RMxaGYjR1/hRDt97+69iJu9hxjAOZSZeCYBid4vAPsdHu+qF/K5Ngr49rnWGFp9Y > g8GkznO7q4L2Lde5WW0GRtNAGTgElEp+9cS1PSxDgyzxEw9+SaJUNLxr5cCsCp8I > dI4Gu88gNgxT6AqomMcrIFd4LyHZ3HEW+rTuFcz4oCcmFQGXHqQVJQV6wbF1NpSf > 8BuEskip4mTOdVvSfciC5jLjCO3q8xT/46TAYwhyrjUecEBR9FGy/e6ImN4DILiU > nd8RvvTMKayAo3OudBcM/dN1YRzc0UijAqhFzV6/thvPNARGobGqzqwE3zKAseLY > sPW1hKbJteDHHLBbG5Ia0/QyqdpXmSkyXjS8dahiuOefjuzWxeerGDaOl3wTiw/S > o3J73WASesJ1RulPjqeW > =M8Zh > -----END PGP SIGNATURE----- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
