diff src/clojure/contrib/cond.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/cond.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,34 @@
     1.4 +;;  Copyright (c) Stephen C. Gilardi. All rights reserved.  The use and
     1.5 +;;  distribution terms for this software are covered by the Eclipse Public
     1.6 +;;  License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
     1.7 +;;  be found in the file epl-v10.html at the root of this distribution.  By
     1.8 +;;  using this software in any fashion, you are agreeing to be bound by the
     1.9 +;;  terms of this license.  You must not remove this notice, or any other,
    1.10 +;;  from this software.
    1.11 +;;
    1.12 +;;  File: cond.clj
    1.13 +;;
    1.14 +;;  scgilardi (gmail)
    1.15 +;;  2 October 2008
    1.16 +
    1.17 +(ns ^{:author "Stephen C. Gilardi"
    1.18 +       :doc "Extensions to the basic cond function."} 
    1.19 +  clojure.contrib.cond)
    1.20 +
    1.21 +(defmacro cond-let
    1.22 +  "Takes a binding-form and a set of test/expr pairs. Evaluates each test
    1.23 +  one at a time. If a test returns logical true, cond-let evaluates and
    1.24 +  returns expr with binding-form bound to the value of test and doesn't
    1.25 +  evaluate any of the other tests or exprs. To provide a default value
    1.26 +  either provide a literal that evaluates to logical true and is
    1.27 +  binding-compatible with binding-form, or use :else as the test and don't
    1.28 +  refer to any parts of binding-form in the expr. (cond-let binding-form)
    1.29 +  returns nil."
    1.30 +  [bindings & clauses]
    1.31 +  (let [binding (first bindings)]
    1.32 +    (when-let [[test expr & more] clauses]
    1.33 +      (if (= test :else)
    1.34 +        expr
    1.35 +        `(if-let [~binding ~test]
    1.36 +           ~expr
    1.37 +           (cond-let ~bindings ~@more))))))