├── .gitattributes ├── .gitignore ├── CONTRIBUTING.md ├── README.md ├── epl.html ├── pom.xml └── src ├── main └── clojure │ └── clojure │ └── core │ ├── incubator.clj │ └── strint.clj └── test └── clojure └── clojure └── core ├── incubator_test.clj └── strint_test.clj /.gitattributes: -------------------------------------------------------------------------------- 1 | epl.html linguist-documentation 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | This is a [Clojure contrib] project. 2 | 3 | Under the Clojure contrib [guidelines], this project cannot accept 4 | pull requests. All patches must be submitted via [JIRA]. 5 | 6 | See [Contributing] on the Clojure website for 7 | more information on how to contribute. 8 | 9 | [Clojure contrib]: https://clojure.org/community/contrib_libs 10 | [Contributing]: https://clojure.org/community/contributing 11 | [JIRA]: http://dev.clojure.org/jira/browse/CINCU 12 | [guidelines]: https://clojure.org/community/contrib_howto 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # THIS PROJECT IS NO LONGER ACTIVE 2 | 3 | # core.incubator 4 | 5 | Future home of fns proposed for core inclusion. Currently: 6 | 7 | * A "nil-safe" threading macro for interop forms (`.?.`) 8 | * An implementation of `dissoc-in` 9 | * An implementation of `seqable?` (deprecated - in core as of Clojure 1.9.0-alpha5) 10 | * A string interpolation macro (`<<`), originally described 11 | [here](https://cemerick.com/blog/2009/12/04/string-interpolation-in-clojure.html). 12 | 13 | NOTE: If you are looking for the other nil-safe threading macros — `-?>` and 14 | `-?>>` — they're still here, but deprecated; they were effectively promoted into 15 | `clojure.core` starting with Clojure 1.5 in the form of `some->` and `some->>`, 16 | respectively. 17 | 18 | For more details, please see the project's [generated API 19 | documentation](http://clojure.github.com/core.incubator/). 20 | 21 | ## "Installation" 22 | 23 | Add a dependency on core.incubator to your Leinigen `project.clj`: 24 | 25 | ```clojure 26 | [org.clojure/core.incubator "0.1.4"] 27 | ``` 28 | 29 | …or to your Maven `pom.xml`: 30 | 31 | ```clojure 32 | 33 | org.clojure 34 | core.incubator 35 | 0.1.4 36 | 37 | ``` 38 | 39 | ## License 40 | 41 | Copyright (c) Rich Hickey and contributors. All rights reserved. 42 | 43 | The use and distribution terms for this software are covered by the 44 | Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 45 | which can be found in the file epl.html at the root of this distribution. 46 | By using this software in any fashion, you are agreeing to be bound by 47 | the terms of this license. 48 | You must not remove this notice, or any other, from this software. 49 | 50 | 51 | -------------------------------------------------------------------------------- /epl.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Eclipse Public License - Version 1.0 8 | 25 | 26 | 27 | 28 | 29 | 30 |

Eclipse Public License - v 1.0

31 | 32 |

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 33 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR 34 | DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS 35 | AGREEMENT.

36 | 37 |

1. DEFINITIONS

38 | 39 |

"Contribution" means:

40 | 41 |

a) in the case of the initial Contributor, the initial 42 | code and documentation distributed under this Agreement, and

43 |

b) in the case of each subsequent Contributor:

44 |

i) changes to the Program, and

45 |

ii) additions to the Program;

46 |

where such changes and/or additions to the Program 47 | originate from and are distributed by that particular Contributor. A 48 | Contribution 'originates' from a Contributor if it was added to the 49 | Program by such Contributor itself or anyone acting on such 50 | Contributor's behalf. Contributions do not include additions to the 51 | Program which: (i) are separate modules of software distributed in 52 | conjunction with the Program under their own license agreement, and (ii) 53 | are not derivative works of the Program.

54 | 55 |

"Contributor" means any person or entity that distributes 56 | the Program.

57 | 58 |

"Licensed Patents" mean patent claims licensable by a 59 | Contributor which are necessarily infringed by the use or sale of its 60 | Contribution alone or when combined with the Program.

61 | 62 |

"Program" means the Contributions distributed in accordance 63 | with this Agreement.

64 | 65 |

"Recipient" means anyone who receives the Program under 66 | this Agreement, including all Contributors.

67 | 68 |

2. GRANT OF RIGHTS

69 | 70 |

a) Subject to the terms of this Agreement, each 71 | Contributor hereby grants Recipient a non-exclusive, worldwide, 72 | royalty-free copyright license to reproduce, prepare derivative works 73 | of, publicly display, publicly perform, distribute and sublicense the 74 | Contribution of such Contributor, if any, and such derivative works, in 75 | source code and object code form.

76 | 77 |

b) Subject to the terms of this Agreement, each 78 | Contributor hereby grants Recipient a non-exclusive, worldwide, 79 | royalty-free patent license under Licensed Patents to make, use, sell, 80 | offer to sell, import and otherwise transfer the Contribution of such 81 | Contributor, if any, in source code and object code form. This patent 82 | license shall apply to the combination of the Contribution and the 83 | Program if, at the time the Contribution is added by the Contributor, 84 | such addition of the Contribution causes such combination to be covered 85 | by the Licensed Patents. The patent license shall not apply to any other 86 | combinations which include the Contribution. No hardware per se is 87 | licensed hereunder.

88 | 89 |

c) Recipient understands that although each Contributor 90 | grants the licenses to its Contributions set forth herein, no assurances 91 | are provided by any Contributor that the Program does not infringe the 92 | patent or other intellectual property rights of any other entity. Each 93 | Contributor disclaims any liability to Recipient for claims brought by 94 | any other entity based on infringement of intellectual property rights 95 | or otherwise. As a condition to exercising the rights and licenses 96 | granted hereunder, each Recipient hereby assumes sole responsibility to 97 | secure any other intellectual property rights needed, if any. For 98 | example, if a third party patent license is required to allow Recipient 99 | to distribute the Program, it is Recipient's responsibility to acquire 100 | that license before distributing the Program.

101 | 102 |

d) Each Contributor represents that to its knowledge it 103 | has sufficient copyright rights in its Contribution, if any, to grant 104 | the copyright license set forth in this Agreement.

105 | 106 |

3. REQUIREMENTS

107 | 108 |

A Contributor may choose to distribute the Program in object code 109 | form under its own license agreement, provided that:

110 | 111 |

a) it complies with the terms and conditions of this 112 | Agreement; and

113 | 114 |

b) its license agreement:

115 | 116 |

i) effectively disclaims on behalf of all Contributors 117 | all warranties and conditions, express and implied, including warranties 118 | or conditions of title and non-infringement, and implied warranties or 119 | conditions of merchantability and fitness for a particular purpose;

120 | 121 |

ii) effectively excludes on behalf of all Contributors 122 | all liability for damages, including direct, indirect, special, 123 | incidental and consequential damages, such as lost profits;

124 | 125 |

iii) states that any provisions which differ from this 126 | Agreement are offered by that Contributor alone and not by any other 127 | party; and

128 | 129 |

iv) states that source code for the Program is available 130 | from such Contributor, and informs licensees how to obtain it in a 131 | reasonable manner on or through a medium customarily used for software 132 | exchange.

133 | 134 |

When the Program is made available in source code form:

135 | 136 |

a) it must be made available under this Agreement; and

137 | 138 |

b) a copy of this Agreement must be included with each 139 | copy of the Program.

140 | 141 |

Contributors may not remove or alter any copyright notices contained 142 | within the Program.

143 | 144 |

Each Contributor must identify itself as the originator of its 145 | Contribution, if any, in a manner that reasonably allows subsequent 146 | Recipients to identify the originator of the Contribution.

147 | 148 |

4. COMMERCIAL DISTRIBUTION

149 | 150 |

Commercial distributors of software may accept certain 151 | responsibilities with respect to end users, business partners and the 152 | like. While this license is intended to facilitate the commercial use of 153 | the Program, the Contributor who includes the Program in a commercial 154 | product offering should do so in a manner which does not create 155 | potential liability for other Contributors. Therefore, if a Contributor 156 | includes the Program in a commercial product offering, such Contributor 157 | ("Commercial Contributor") hereby agrees to defend and 158 | indemnify every other Contributor ("Indemnified Contributor") 159 | against any losses, damages and costs (collectively "Losses") 160 | arising from claims, lawsuits and other legal actions brought by a third 161 | party against the Indemnified Contributor to the extent caused by the 162 | acts or omissions of such Commercial Contributor in connection with its 163 | distribution of the Program in a commercial product offering. The 164 | obligations in this section do not apply to any claims or Losses 165 | relating to any actual or alleged intellectual property infringement. In 166 | order to qualify, an Indemnified Contributor must: a) promptly notify 167 | the Commercial Contributor in writing of such claim, and b) allow the 168 | Commercial Contributor to control, and cooperate with the Commercial 169 | Contributor in, the defense and any related settlement negotiations. The 170 | Indemnified Contributor may participate in any such claim at its own 171 | expense.

172 | 173 |

For example, a Contributor might include the Program in a commercial 174 | product offering, Product X. That Contributor is then a Commercial 175 | Contributor. If that Commercial Contributor then makes performance 176 | claims, or offers warranties related to Product X, those performance 177 | claims and warranties are such Commercial Contributor's responsibility 178 | alone. Under this section, the Commercial Contributor would have to 179 | defend claims against the other Contributors related to those 180 | performance claims and warranties, and if a court requires any other 181 | Contributor to pay any damages as a result, the Commercial Contributor 182 | must pay those damages.

183 | 184 |

5. NO WARRANTY

185 | 186 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 187 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 188 | OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 189 | ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 190 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 191 | responsible for determining the appropriateness of using and 192 | distributing the Program and assumes all risks associated with its 193 | exercise of rights under this Agreement , including but not limited to 194 | the risks and costs of program errors, compliance with applicable laws, 195 | damage to or loss of data, programs or equipment, and unavailability or 196 | interruption of operations.

197 | 198 |

6. DISCLAIMER OF LIABILITY

199 | 200 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT 201 | NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 202 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 203 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 204 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 205 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 206 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 207 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

208 | 209 |

7. GENERAL

210 | 211 |

If any provision of this Agreement is invalid or unenforceable under 212 | applicable law, it shall not affect the validity or enforceability of 213 | the remainder of the terms of this Agreement, and without further action 214 | by the parties hereto, such provision shall be reformed to the minimum 215 | extent necessary to make such provision valid and enforceable.

216 | 217 |

If Recipient institutes patent litigation against any entity 218 | (including a cross-claim or counterclaim in a lawsuit) alleging that the 219 | Program itself (excluding combinations of the Program with other 220 | software or hardware) infringes such Recipient's patent(s), then such 221 | Recipient's rights granted under Section 2(b) shall terminate as of the 222 | date such litigation is filed.

223 | 224 |

All Recipient's rights under this Agreement shall terminate if it 225 | fails to comply with any of the material terms or conditions of this 226 | Agreement and does not cure such failure in a reasonable period of time 227 | after becoming aware of such noncompliance. If all Recipient's rights 228 | under this Agreement terminate, Recipient agrees to cease use and 229 | distribution of the Program as soon as reasonably practicable. However, 230 | Recipient's obligations under this Agreement and any licenses granted by 231 | Recipient relating to the Program shall continue and survive.

232 | 233 |

Everyone is permitted to copy and distribute copies of this 234 | Agreement, but in order to avoid inconsistency the Agreement is 235 | copyrighted and may only be modified in the following manner. The 236 | Agreement Steward reserves the right to publish new versions (including 237 | revisions) of this Agreement from time to time. No one other than the 238 | Agreement Steward has the right to modify this Agreement. The Eclipse 239 | Foundation is the initial Agreement Steward. The Eclipse Foundation may 240 | assign the responsibility to serve as the Agreement Steward to a 241 | suitable separate entity. Each new version of the Agreement will be 242 | given a distinguishing version number. The Program (including 243 | Contributions) may always be distributed subject to the version of the 244 | Agreement under which it was received. In addition, after a new version 245 | of the Agreement is published, Contributor may elect to distribute the 246 | Program (including its Contributions) under the new version. Except as 247 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives no 248 | rights or licenses to the intellectual property of any Contributor under 249 | this Agreement, whether expressly, by implication, estoppel or 250 | otherwise. All rights in the Program not expressly granted under this 251 | Agreement are reserved.

252 | 253 |

This Agreement is governed by the laws of the State of New York and 254 | the intellectual property laws of the United States of America. No party 255 | to this Agreement will bring a legal action under this Agreement more 256 | than one year after the cause of action arose. Each party waives its 257 | rights to a jury trial in any resulting litigation.

258 | 259 | 260 | 261 | 262 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | core.incubator 4 | 0.1.5-SNAPSHOT 5 | core.incubator 6 | 7 | 8 | org.clojure 9 | pom.contrib 10 | 0.2.2 11 | 12 | 13 | 14 | scm:git:git@github.com:clojure/core.incubator.git 15 | scm:git:git@github.com:clojure/core.incubator.git 16 | git@github.com:clojure/core.incubator.git 17 | HEAD 18 | 19 | 20 | 21 | true 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/clojure/clojure/core/incubator.clj: -------------------------------------------------------------------------------- 1 | ; Copyright (c) Laurent Petit and others, March 2009. All rights reserved. 2 | 3 | ; The use and distribution terms for this software are covered by the 4 | ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ; which can be found in the file epl-v10.html at the root of this 6 | ; distribution. 7 | ; By using this software in any fashion, you are agreeing to be bound by 8 | ; the terms of this license. 9 | ; You must not remove this notice, or any other, from this software. 10 | 11 | (ns ^{:author "Laurent Petit (and others)" 12 | :doc "Functions/macros variants of the ones that can be found in clojure.core 13 | (note to other contrib members: feel free to add to this lib)"} 14 | clojure.core.incubator 15 | (:refer-clojure :exclude [seqable?])) 16 | 17 | (defmacro defmacro- 18 | "Same as defmacro but yields a private definition" 19 | [name & decls] 20 | (list* `defmacro (with-meta name (assoc (meta name) :private true)) decls)) 21 | 22 | (defmacro- defnilsafe [docstring non-safe-name nil-safe-name] 23 | `(defmacro ~nil-safe-name ~docstring 24 | {:arglists '([~'x ~'form] [~'x ~'form ~'& ~'forms])} 25 | ([x# form#] 26 | `(let [~'i# ~x#] (when-not (nil? ~'i#) (~'~non-safe-name ~'i# ~form#)))) 27 | ([x# form# & more#] 28 | `(~'~nil-safe-name (~'~nil-safe-name ~x# ~form#) ~@more#)))) 29 | 30 | (defnilsafe 31 | "DEPRECATED: use clojure.core/some-> instead. 32 | 33 | Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation). 34 | Examples : 35 | (-?> \"foo\" .toUpperCase (.substring 1)) returns \"OO\" 36 | (-?> nil .toUpperCase (.substring 1)) returns nil 37 | " 38 | -> 39 | ^{:deprecated "0.1.3"} -?>) 40 | 41 | (defnilsafe 42 | "Same as clojure.core/.. but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation). 43 | Examples : 44 | (.?. \"foo\" .toUpperCase (.substring 1)) returns \"OO\" 45 | (.?. nil .toUpperCase (.substring 1)) returns nil 46 | " 47 | .. .?.) 48 | 49 | (defnilsafe 50 | "DEPRECATED: use clojure.core/some->> instead. 51 | 52 | Same as clojure.core/->> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation). 53 | Examples : 54 | (-?>> (range 5) (map inc)) returns (1 2 3 4 5) 55 | (-?>> [] seq (map inc)) returns nil 56 | " 57 | ->> 58 | ^{:deprecated "0.1.3"} -?>>) 59 | 60 | ;; ---------------------------------------------------------------------- 61 | ;; scgilardi at gmail 62 | 63 | (defn dissoc-in 64 | "Dissociates an entry from a nested associative structure returning a new 65 | nested structure. keys is a sequence of keys. Any empty maps that result 66 | will not be present in the new structure." 67 | [m [k & ks :as keys]] 68 | (if ks 69 | (if-let [nextmap (get m k)] 70 | (let [newmap (dissoc-in nextmap ks)] 71 | (if (seq newmap) 72 | (assoc m k newmap) 73 | (dissoc m k))) 74 | m) 75 | (dissoc m k))) 76 | 77 | (defn new-by-name 78 | "Constructs a Java object whose class is specified by a String." 79 | [class-name & args] 80 | (clojure.lang.Reflector/invokeConstructor 81 | (clojure.lang.RT/classForName class-name) 82 | (into-array Object args))) 83 | 84 | (defn seqable? 85 | "DEPRECATED: use clojure.core/seqable? as of Clojure 1.9.0-alpha5. 86 | 87 | Returns true if (seq x) will succeed, false otherwise." 88 | {:deprecated "0.1.4"} 89 | [x] 90 | (or (seq? x) 91 | (instance? clojure.lang.Seqable x) 92 | (nil? x) 93 | (instance? Iterable x) 94 | (.isArray (.getClass ^Object x)) 95 | (string? x) 96 | (instance? java.util.Map x))) 97 | -------------------------------------------------------------------------------- /src/main/clojure/clojure/core/strint.clj: -------------------------------------------------------------------------------- 1 | ;;; strint.clj -- String interpolation for Clojure 2 | ;; originally proposed/published at http://cemerick.com/2009/12/04/string-interpolation-in-clojure/ 3 | 4 | ;; by Chas Emerick 5 | ;; December 4, 2009 6 | 7 | ;; Copyright (c) Chas Emerick, 2009. All rights reserved. The use 8 | ;; and distribution terms for this software are covered by the Eclipse 9 | ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 10 | ;; which can be found in the file epl-v10.html at the root of this 11 | ;; distribution. By using this software in any fashion, you are 12 | ;; agreeing to be bound by the terms of this license. You must not 13 | ;; remove this notice, or any other, from this software. 14 | 15 | (ns 16 | ^{:author "Chas Emerick", 17 | :doc "Compile-time string interpolation for Clojure."} 18 | clojure.core.strint) 19 | 20 | (defn- silent-read 21 | "Attempts to clojure.core/read a single form from the provided String, returning 22 | a vector containing the read form and a String containing the unread remainder 23 | of the provided String. Returns nil if no valid form can be read from the 24 | head of the String." 25 | [s] 26 | (try 27 | (let [r (-> s java.io.StringReader. java.io.PushbackReader.)] 28 | [(read r) (slurp r)]) 29 | (catch Exception e))) ; this indicates an invalid form -- the head of s is just string data 30 | 31 | (defn- interpolate 32 | "Yields a seq of Strings and read forms." 33 | ([s atom?] 34 | (lazy-seq 35 | (if-let [[form rest] (silent-read (subs s (if atom? 2 1)))] 36 | (cons form (interpolate (if atom? (subs rest 1) rest))) 37 | (cons (subs s 0 2) (interpolate (subs s 2)))))) 38 | ([^String s] 39 | (if-let [start (->> ["~{" "~("] 40 | (map #(.indexOf s ^String %)) 41 | (remove #(== -1 %)) 42 | sort 43 | first)] 44 | (lazy-seq (cons 45 | (subs s 0 start) 46 | (interpolate (subs s start) (= \{ (.charAt s (inc start)))))) 47 | [s]))) 48 | 49 | (defmacro << 50 | "Accepts one or more strings; emits a `str` invocation that concatenates 51 | the string data and evaluated expressions contained within that argument. 52 | Evaluation is controlled using ~{} and ~() forms. The former is used for 53 | simple value replacement using clojure.core/str; the latter can be used to 54 | embed the results of arbitrary function invocation into the produced string. 55 | 56 | Examples: 57 | user=> (def v 30.5) 58 | #'user/v 59 | user=> (<< \"This trial required ~{v}ml of solution.\") 60 | \"This trial required 30.5ml of solution.\" 61 | user=> (<< \"There are ~(int v) days in November.\") 62 | \"There are 30 days in November.\" 63 | user=> (def m {:a [1 2 3]}) 64 | #'user/m 65 | user=> (<< \"The total for your order is $~(->> m :a (apply +)).\") 66 | \"The total for your order is $6.\" 67 | user=> (<< \"Just split a long interpolated string up into ~(-> m :a (get 0)), \" 68 | \"~(-> m :a (get 1)), or even ~(-> m :a (get 2)) separate strings \" 69 | \"if you don't want a << expression to end up being e.g. ~(* 4 (int v)) \" 70 | \"columns wide.\") 71 | \"Just split a long interpolated string up into 1, 2, or even 3 separate strings if you don't want a << expression to end up being e.g. 120 columns wide.\" 72 | 73 | Note that quotes surrounding string literals within ~() forms must be 74 | escaped." 75 | [& strings] 76 | `(str ~@(interpolate (apply str strings)))) 77 | -------------------------------------------------------------------------------- /src/test/clojure/clojure/core/incubator_test.clj: -------------------------------------------------------------------------------- 1 | ; Copyright (c) Laurent Petit, March 2009. All rights reserved. 2 | 3 | ; The use and distribution terms for this software are covered by the 4 | ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ; which can be found in the file epl-v10.html at the root of this 6 | ; distribution. 7 | ; By using this software in any fashion, you are agreeing to be bound by 8 | ; the terms of this license. 9 | ; You must not remove this notice, or any other, from this software. 10 | 11 | (ns clojure.core.incubator-test 12 | (:use clojure.test 13 | clojure.core.incubator)) 14 | 15 | (deftest test-classic-versions 16 | (testing "Classic -> throws NPE if passed nil" 17 | (is (thrown? NullPointerException (-> nil .toString))) 18 | (is (thrown? NullPointerException (-> "foo" seq next next next .toString)))) 19 | (testing "Classic .. throws NPE if one of the intermediate threaded values is nil" 20 | (is (thrown? NullPointerException (.. nil toString))) 21 | (is (thrown? NullPointerException (.. [nil] (get 0) toString))))) 22 | 23 | (deftest test-new-versions 24 | (testing "Version -?>> falls out on nil" 25 | (is (nil? (-?>> nil .toString))) 26 | (is (nil? (-?>> [] seq (map inc)))) 27 | (is (= [] (->> [] seq (map inc))))) 28 | (testing "Version -?>> completes for non-nil" 29 | (is (= [3 4] (-?>> [1 2] (map inc) (map inc))))) 30 | (testing "Version -?> falls out on nil" 31 | (is (nil? (-?> nil .toString))) 32 | (is (nil? (-?> "foo" seq next next next .toString)))) 33 | (testing "Version -?> completes for non-nil" 34 | (is (= [\O \O] (-?> "foo" .toUpperCase rest)))) 35 | (testing "Version .?. returns nil if one of the intermediate threaded values is nil" 36 | (is (nil? (.?. nil toString))) 37 | (is (nil? (.?. [nil] (get 0) toString))))) -------------------------------------------------------------------------------- /src/test/clojure/clojure/core/strint_test.clj: -------------------------------------------------------------------------------- 1 | ; Copyright (c) Chas Emerick 2009. All rights reserved. 2 | 3 | ; The use and distribution terms for this software are covered by the 4 | ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ; which can be found in the file epl-v10.html at the root of this 6 | ; distribution. 7 | ; By using this software in any fashion, you are agreeing to be bound by 8 | ; the terms of this license. 9 | ; You must not remove this notice, or any other, from this software. 10 | 11 | (ns clojure.core.strint-test 12 | (:use clojure.test 13 | clojure.core.strint)) 14 | 15 | (deftest test-silent-read 16 | (let [silent-read @#'clojure.core.strint/silent-read] 17 | (testing "reading a valid form returns [read form, rest of string]" 18 | (is (= [[1] "[2]"] (silent-read "[1][2]")))) 19 | (testing "reading an invalid form returns nil" 20 | (is (= nil (silent-read "[")))))) 21 | 22 | (deftest test-interpolate 23 | (let [interpolate @#'clojure.core.strint/interpolate] 24 | (testing "a plain old string" 25 | (is (= ["a plain old string"] (interpolate "a plain old string")))) 26 | (testing "some value replacement forms" 27 | (is (= '["" foo " and " bar ""] (interpolate "~{foo} and ~{bar}")))) 28 | (testing "some fn-calling forms" 29 | (is (= '["" (+ 1 2) " and " (vector 3) ""] (interpolate "~(+ 1 2) and ~(vector 3)")))))) 30 | 31 | (deftest test-<< 32 | (testing "docstring examples" 33 | (let [v 30.5 34 | m {:a [1 2 3]}] 35 | (is (= "This trial required 30.5ml of solution." 36 | (<< "This trial required ~{v}ml of solution."))) 37 | (is (= "There are 30 days in November." 38 | (<< "There are ~(int v) days in November."))) 39 | (is (= "The total for your order is $6." 40 | (<< "The total for your order is $~(->> m :a (apply +)).")))))) 41 | 42 | (deftest variadic-<< 43 | (testing "docstring examples, split across multiple string literals" 44 | (let [v 30.5 45 | m {:a [1 2 3]}] 46 | (is (= "This trial required 30.5ml of solution." 47 | (<< "This trial required ~{" "v}ml of solution."))) 48 | (is (= "There are 30 days in November." 49 | (<< "There are ~(" "int " "v) days in November."))) 50 | (is (= "The total for your order is $6." 51 | (<< "The total " "for your order is" " $~(->> m :a (apply " "+))." ""))) 52 | (is (= (str "Just split a long interpolated string up into 1, 2, or even 3 " 53 | "separate strings if you don't want a << expression to end up being " 54 | "e.g. 120 columns wide.") 55 | (<< "Just split a long interpolated string up into ~(-> m :a (get 0)), " 56 | "~(-> m :a (get 1)), or even ~(-> m :a (get 2)) separate strings " 57 | "if you don't want a << expression to end up being e.g. ~(* 4 (int v)) " 58 | "columns wide.")))))) --------------------------------------------------------------------------------