|
357 | 357 |
|
358 | 358 | (defn symbol |
359 | 359 | "Returns a Symbol with the given namespace and name." |
| 360 | + {:tag clojure.lang.Symbol} |
360 | 361 | ([name] (if (symbol? name) name (clojure.lang.Symbol/intern name))) |
361 | 362 | ([ns name] (clojure.lang.Symbol/intern ns name))) |
362 | 363 |
|
363 | 364 | (defn keyword |
364 | 365 | "Returns a Keyword with the given namespace and name. Do not use : |
365 | 366 | in the keyword strings, it will be added automatically." |
| 367 | + {:tag clojure.lang.Keyword} |
366 | 368 | ([name] (if (keyword? name) name (clojure.lang.Keyword/intern name))) |
367 | 369 | ([ns name] (clojure.lang.Keyword/intern ns name))) |
368 | 370 |
|
|
540 | 542 |
|
541 | 543 |
|
542 | 544 | (defn compare |
543 | | - "Comparator. Returns 0 if x equals y, -1 if x is logically 'less |
544 | | - than' y, else 1. Same as Java x.compareTo(y) except it also works |
545 | | - for nil, and compares numbers and collections in a type-independent |
546 | | - manner. x must implement Comparable" |
| 545 | + "Comparator. Returns a negative number, zero, or a positive number |
| 546 | + when x is logically 'less than', 'equal to', or 'greater than' |
| 547 | + y. Same as Java x.compareTo(y) except it also works for nil, and |
| 548 | + compares numbers and collections in a type-independent manner. x |
| 549 | + must implement Comparable" |
547 | 550 | {:tag Integer |
548 | 551 | :inline (fn [x y] `(. clojure.lang.Util compare ~x ~y))} |
549 | 552 | [x y] (. clojure.lang.Util (compare x y))) |
|
1070 | 1073 | (list form x))) |
1071 | 1074 | ([x form & more] `(-> (-> ~x ~form) ~@more))) |
1072 | 1075 |
|
| 1076 | +(defmacro ->> |
| 1077 | + "Threads the expr through the forms. Inserts x as the |
| 1078 | + last item in the first form, making a list of it if it is not a |
| 1079 | + list already. If there are more forms, inserts the first form as the |
| 1080 | + last item in second form, etc." |
| 1081 | + ([x form] (if (seq? form) |
| 1082 | + `(~(first form) ~@(next form) ~x) |
| 1083 | + (list form x))) |
| 1084 | + ([x form & more] `(->> (->> ~x ~form) ~@more))) |
| 1085 | + |
1073 | 1086 | ;;multimethods |
1074 | 1087 | (def global-hierarchy) |
1075 | 1088 |
|
|
1181 | 1194 | (let [~form temp#] |
1182 | 1195 | ~@body))))) |
1183 | 1196 |
|
| 1197 | +(defn push-thread-bindings |
| 1198 | + "WARNING: This is a low-level function. Prefer high-level macros like |
| 1199 | + binding where ever possible. |
| 1200 | +
|
| 1201 | + Takes a map of Var/value pairs. Binds each Var to the associated value for |
| 1202 | + the current thread. Each call *MUST* be accompanied by a matching call to |
| 1203 | + pop-thread-bindings wrapped in a try-finally! |
| 1204 | + |
| 1205 | + (push-thread-bindings bindings) |
| 1206 | + (try |
| 1207 | + ... |
| 1208 | + (finally |
| 1209 | + (pop-thread-bindings)))" |
| 1210 | + [bindings] |
| 1211 | + (clojure.lang.Var/pushThreadBindings bindings)) |
| 1212 | + |
| 1213 | +(defn pop-thread-bindings |
| 1214 | + "Pop one set of bindings pushed with push-binding before. It is an error to |
| 1215 | + pop bindings without pushing before." |
| 1216 | + [] |
| 1217 | + (clojure.lang.Var/popThreadBindings)) |
| 1218 | + |
| 1219 | +(defn get-thread-bindings |
| 1220 | + "Get a map with the Var/value pairs which is currently in effect for the |
| 1221 | + current thread." |
| 1222 | + [] |
| 1223 | + (clojure.lang.Var/getThreadBindings)) |
| 1224 | + |
1184 | 1225 | (defmacro binding |
1185 | 1226 | "binding => var-symbol init-expr |
1186 | 1227 |
|
1187 | 1228 | Creates new bindings for the (already-existing) vars, with the |
1188 | 1229 | supplied initial values, executes the exprs in an implicit do, then |
1189 | | - re-establishes the bindings that existed before." |
| 1230 | + re-establishes the bindings that existed before. The new bindings |
| 1231 | + are made in parallel (unlike let); all init-exprs are evaluated |
| 1232 | + before the vars are bound to their new values." |
1190 | 1233 | [bindings & body] |
1191 | | - (assert-args binding |
1192 | | - (vector? bindings) "a vector for its binding" |
1193 | | - (even? (count bindings)) "an even number of forms in binding vector") |
1194 | | - (let [var-ize (fn [var-vals] |
1195 | | - (loop [ret [] vvs (seq var-vals)] |
1196 | | - (if vvs |
1197 | | - (recur (conj (conj ret `(var ~(first vvs))) (second vvs)) |
1198 | | - (next (next vvs))) |
1199 | | - (seq ret))))] |
1200 | | - `(let [] |
1201 | | - (. clojure.lang.Var (pushThreadBindings (hash-map ~@(var-ize bindings)))) |
1202 | | - (try |
1203 | | - ~@body |
1204 | | - (finally |
1205 | | - (. clojure.lang.Var (popThreadBindings))))))) |
| 1234 | + (assert-args binding |
| 1235 | + (vector? bindings) "a vector for its binding" |
| 1236 | + (even? (count bindings)) "an even number of forms in binding vector") |
| 1237 | + (let [var-ize (fn [var-vals] |
| 1238 | + (loop [ret [] vvs (seq var-vals)] |
| 1239 | + (if vvs |
| 1240 | + (recur (conj (conj ret `(var ~(first vvs))) (second vvs)) |
| 1241 | + (next (next vvs))) |
| 1242 | + (seq ret))))] |
| 1243 | + `(let [] |
| 1244 | + (push-thread-bindings (hash-map ~@(var-ize bindings))) |
| 1245 | + (try |
| 1246 | + ~@body |
| 1247 | + (finally |
| 1248 | + (pop-thread-bindings)))))) |
1206 | 1249 |
|
1207 | 1250 | (defn find-var |
1208 | 1251 | "Returns the global var named by the namespace-qualified symbol, or |
|
1759 | 1802 | ([s] (drop-last 1 s)) |
1760 | 1803 | ([n s] (map (fn [x _] x) s (drop n s)))) |
1761 | 1804 |
|
| 1805 | +(defn take-last |
| 1806 | + "Returns a seq of the last n items in coll. Depending on the type |
| 1807 | + of coll may be no better than linear time. For vectors, see also subvec." |
| 1808 | + [n coll] |
| 1809 | + (loop [s (seq coll), lead (seq (drop n coll))] |
| 1810 | + (if lead |
| 1811 | + (recur (next s) (next lead)) |
| 1812 | + s))) |
| 1813 | + |
1762 | 1814 | (defn drop-while |
1763 | 1815 | "Returns a lazy sequence of the items in coll starting from the first |
1764 | 1816 | item for which (pred item) returns nil." |
|
2936 | 2988 | (conj (pop groups) (conj (peek groups) [k v])) |
2937 | 2989 | (conj groups [k v]))) |
2938 | 2990 | [] (partition 2 seq-exprs))) |
2939 | | - err (fn [& msg] (throw (IllegalArgumentException. (apply str msg)))) |
| 2991 | + err (fn [& msg] (throw (IllegalArgumentException. #^String (apply str msg)))) |
2940 | 2992 | emit-bind (fn emit-bind [[[bind expr & mod-pairs] |
2941 | 2993 | & [[_ next-expr] :as next-groups]]] |
2942 | 2994 | (let [giter (gensym "iter__") |
|
3728 | 3780 | (defmacro with-loading-context [& body] |
3729 | 3781 | `((fn loading# [] |
3730 | 3782 | (. clojure.lang.Var (pushThreadBindings {clojure.lang.Compiler/LOADER |
3731 | | - (-> loading# .getClass .getClassLoader)})) |
| 3783 | + (.getClassLoader (.getClass #^Object loading#))})) |
3732 | 3784 | (try |
3733 | 3785 | ~@body |
3734 | 3786 | (finally |
|
4403 | 4455 | "clojure/version.properties") |
4404 | 4456 | properties (doto (new java.util.Properties) (.load version-stream)) |
4405 | 4457 | prop (fn [k] (.getProperty properties (str "clojure.version." k))) |
4406 | | - clojure-version {:major (Integer/valueOf (prop "major")) |
4407 | | - :minor (Integer/valueOf (prop "minor")) |
4408 | | - :incremental (Integer/valueOf (prop "incremental")) |
| 4458 | + clojure-version {:major (Integer/valueOf #^String (prop "major")) |
| 4459 | + :minor (Integer/valueOf #^String (prop "minor")) |
| 4460 | + :incremental (Integer/valueOf #^String (prop "incremental")) |
4409 | 4461 | :qualifier (prop "qualifier")}] |
4410 | 4462 | (def *clojure-version* |
4411 | 4463 | (if (not (= (prop "interim") "false")) |
|
4502 | 4554 | [#^clojure.lang.ITransientVector coll] |
4503 | 4555 | (.pop coll)) |
4504 | 4556 |
|
| 4557 | +(defn disj! |
| 4558 | + "disj[oin]. Returns a transient set of the same (hashed/sorted) type, that |
| 4559 | + does not contain key(s)." |
| 4560 | + ([set] set) |
| 4561 | + ([#^clojure.lang.ITransientSet set key] |
| 4562 | + (. set (disjoin key))) |
| 4563 | + ([set key & ks] |
| 4564 | + (let [ret (disj set key)] |
| 4565 | + (if ks |
| 4566 | + (recur ret (first ks) (next ks)) |
| 4567 | + ret)))) |
| 4568 | + |
4505 | 4569 | ;redef into with batch support |
4506 | 4570 | (defn into |
4507 | 4571 | "Returns a new coll consisting of to-coll with all of the items of |
|
4516 | 4580 | (if items |
4517 | 4581 | (recur (conj ret (first items)) (next items)) |
4518 | 4582 | ret))))) |
| 4583 | + |
0 commit comments