Ответ 1
Существует несколько возможностей - здесь несколько для запуска:
;;; 1. direct translation
; _ is the idiomatic "I don't care" identifier in Clojure
(let [a (get-a)
_ (if (= a bad-value) (throw (Exception. "Foo!")))
b (get-b)
_ (if (= b bad-value) (throw (Exception. "Foo!")))
...]
(do-action-with a b ...))
;;; 2. abstract the pattern away
(defmacro disallow
([expr val] ; binary version with default exception type;
; omit if explicit type is to be required
(disallow (list* &form [Exception]) &env expr val Exception))
([expr val e]
`(let [actual# ~expr]
(if (= actual# ~val)
(throw (new ~e (str "Value " ~val " not allowed.")))
actual#))))
(let [a (disallow (get-a) ExceptionA)
b (disallow (get-b) ExceptionB)
...]
...)
;;; 3. monadic short-circuiting
(use '[clojure.contrib.monads :only [domonad maybe-m]])
; ...now do it more or less as in Haskell;
; you can use :when in domonad for monads with m-zero
; -- the syntax is that of for / doseq:
(doseq [sym '[a b c]] (intern *ns* sym (atom 0)))
(domonad maybe-m
[a @a
:when (pos? a)
b @b
:when (neg? b)
c @c
:when (not (zero? c))]
(* a b c))
; => 0
(dorun (map reset! [a b c] [3 -2 1]))
(domonad maybe-m
; same as above
)
; => -6