├── CHANGELOG.md ├── .gitignore ├── project.clj ├── LICENSE ├── src └── com │ └── walmartlabs │ └── cond_let.clj ├── test └── com │ └── walmartlabs │ └── cond_let_tests.clj └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.0.0 - 3 Oct 2018 2 | 3 | Initial release of the library. 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject com.walmartlabs/cond-let "1.0.0" 2 | :description "cond-let: a macro that merges cond and let" 3 | :url "https://github.com/walmartlabs/cond-let" 4 | :license {:name "Apache Software License 2.0" 5 | :url "http://www.apache.org/licenses/LICENSE-2.0"} 6 | :dependencies [[org.clojure/clojure "1.8.0"]] 7 | :plugins [[lein-codox "0.10.3"]] 8 | :codox {:source-uri "https://github.com/walmartlabs/cond-let/blob/master/{filepath}#L{line}" 9 | :metadata {:doc/format :markdown}}) 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 WalmartLabs 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /src/com/walmartlabs/cond_let.clj: -------------------------------------------------------------------------------- 1 | ; 2 | ; Copyright (c) 2018-present, Walmart Inc 3 | ; 4 | ; Licensed under the Apache License, Version 2.0 (the "License") 5 | ; you may not use this file except in compliance with the License. 6 | ; You may obtain a copy of the License at 7 | ; 8 | ; http://www.apache.org/licenses/LICENSE-2.0 9 | ; 10 | ; Unless required by applicable law or agreed to in writing, software 11 | ; distributed under the License is distributed on an "AS IS" BASIS, 12 | ; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | ; See the License for the specific language governing permissions and 14 | ; limitations under the License. 15 | ; 16 | ; 17 | 18 | (ns com.walmartlabs.cond-let 19 | "Home for the cond-let macro.") 20 | 21 | (defmacro cond-let 22 | "An alternative to `clojure.core/cond` where instead of a test/expression pair, it is possible 23 | to have a :let/binding vector pair." 24 | [& clauses] 25 | (cond (empty? clauses) 26 | nil 27 | 28 | (not (even? (count clauses))) 29 | (throw (ex-info (str `cond-let " requires an even number of forms") 30 | {:form &form 31 | :meta (meta &form)})) 32 | 33 | :else 34 | (let [[test expr-or-binding-form & more-clauses] clauses] 35 | (if (= :let test) 36 | `(let ~expr-or-binding-form (cond-let ~@more-clauses)) 37 | ;; Standard case 38 | `(if ~test 39 | ~expr-or-binding-form 40 | (cond-let ~@more-clauses)))))) 41 | -------------------------------------------------------------------------------- /test/com/walmartlabs/cond_let_tests.clj: -------------------------------------------------------------------------------- 1 | ; 2 | ; Copyright (c) 2018-present, Walmart Inc 3 | ; 4 | ; Licensed under the Apache License, Version 2.0 (the "License") 5 | ; you may not use this file except in compliance with the License. 6 | ; You may obtain a copy of the License at 7 | ; 8 | ; http://www.apache.org/licenses/LICENSE-2.0 9 | ; 10 | ; Unless required by applicable law or agreed to in writing, software 11 | ; distributed under the License is distributed on an "AS IS" BASIS, 12 | ; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | ; See the License for the specific language governing permissions and 14 | ; limitations under the License. 15 | ; 16 | ; 17 | 18 | (ns com.walmartlabs.cond-let-tests 19 | (:require 20 | [com.walmartlabs.cond-let :refer [cond-let]] 21 | [clojure.test :refer [deftest is]]) 22 | (:import (clojure.lang ExceptionInfo))) 23 | 24 | 25 | (deftest basic-case 26 | (is (= 1 27 | (cond-let 28 | false 29 | nil 30 | 31 | :let [x 1 32 | y false] 33 | 34 | y 35 | :y 36 | 37 | :else 38 | x)))) 39 | 40 | (deftest empty-is-nil 41 | (is (nil? (cond-let)))) 42 | 43 | (deftest checks-number-of-forms 44 | (is (thrown-with-msg? ExceptionInfo #"requires an even number of forms" 45 | (eval `(cond-let false 46 | nil 47 | 48 | :let [x 1] 49 | 50 | ;; Note: missing an :else or something here 51 | x))))) 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # com.walmartlabs/cond-let 2 | 3 | [![Clojars Project](https://img.shields.io/clojars/v/com.walmartlabs/cond-let.svg)](https://clojars.org/com.walmartlabs/cond-let) 4 | 5 | > Like this? By publishing it, I found out about [better-cond](https://github.com/Engelberg/better-cond) which has a super-set of the features here. 6 | Go use it with my blessing! I do! 7 | 8 | 9 | A micro-library around the useful `cond-let` macro. 10 | 11 | `cond-let` acts like a `cond`, but adds 12 | `:let` terms that are followed by a binding form (like `let`). 13 | 14 | This allows conditional code to introduce new local symbols; the result 15 | is clearer, more linear code, that doesn't make a march for the 16 | right margin. 17 | 18 | [API Documentation](http://walmartlabs.github.io/apidocs/cond-let/) 19 | 20 | ## Usage 21 | 22 | An example from the Clockwork library: 23 | 24 | ```clj 25 | (defn ^:private has-necessary-capabilities? 26 | "Does the worker have all the capabilities that the job needs?" 27 | [state worker-id task] 28 | (cond-let 29 | 30 | :let [job-id (:job-id task)] 31 | 32 | (nil? job-id) 33 | true 34 | 35 | :let [capabilities (get-in state [:jobs job-id :clockwork/required-capabilities])] 36 | 37 | (empty? capabilities) 38 | true 39 | 40 | :let [worker-capabilities (get-in state [:workers worker-id :capabilities])] 41 | 42 | (empty? worker-capabilities) 43 | false 44 | 45 | :else 46 | ;; It's ok for the worker to have *more* capabilities than are specified. 47 | ;; For each required capability, we need an exact match. 48 | (= (select-keys worker-capabilities (keys capabilities)) 49 | capabilities))) 50 | ``` 51 | 52 | ## License 53 | 54 | Copyright © 2018 Walmart 55 | 56 | Distributed under the Apache Software License 2.0. 57 | --------------------------------------------------------------------------------