diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-03-10 03:10:05 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-03-10 03:10:05 +0000 |
commit | cbc24c18cb0a5986abc2e34bfa2b76aed5f00833 (patch) | |
tree | 519397842c68d5766d13aba45aa95a599a4cc6d7 /src | |
parent | 6d38cdc995cb5f0adef2b0f5da9f4db4b9701ae9 (diff) |
moving to new metadata/docstrings
Diffstat (limited to 'src')
-rw-r--r-- | src/boot.clj | 1727 |
1 files changed, 837 insertions, 890 deletions
diff --git a/src/boot.clj b/src/boot.clj index a45b8274..fb3d30d0 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -1051,98 +1051,89 @@ not-every? (comp not every?)) (recur (seq coll))))] (rep (seq coll))))) -(defn - #^{:doc "Returns a vector of [(take n coll) (drop n coll)]"} -split-at [n coll] - [(take n coll) (drop n coll)]) +(defn split-at + "Returns a vector of [(take n coll) (drop n coll)]" + [n coll] + [(take n coll) (drop n coll)]) -(defn - #^{:doc "Returns a vector of [(take-while pred coll) (drop-while pred coll)]"} -split-with [pred coll] - [(take-while pred coll) (drop-while pred coll)]) +(defn split-with + "Returns a vector of [(take-while pred coll) (drop-while pred coll)]" + [pred coll] + [(take-while pred coll) (drop-while pred coll)]) -(defn - #^{:doc "Returns a lazy (infinite!) seq of xs."} -repeat [x] - (lazy-cons x (repeat x))) +(defn repeat + "Returns a lazy (infinite!) seq of xs." + [x] (lazy-cons x (repeat x))) -(defn - #^{:doc "Returns a lazy seq of n xs."} -replicate [n x] - (take n (repeat x))) +(defn replicate + "Returns a lazy seq of n xs." + [n x] (take n (repeat x))) -(defn - #^{:doc "Returns a lazy seq of x, (f x), (f (f x)) etc."} -iterate [f x] - (lazy-cons x (iterate f (f x)))) - -(defn - #^{:doc "Returns a lazy seq of nums from start (inclusive) to end - (exclusive), by step, where start defaults to 0 and step - to 1."} -range - ([end] (take end (iterate inc 0))) - ([start end] (take (- end start) (iterate inc start))) - ([start end step] +(defn iterate + "Returns a lazy seq of x, (f x), (f (f x)) etc." + [f x] (lazy-cons x (iterate f (f x)))) + +(defn range + "Returns a lazy seq of nums from start (inclusive) to end + (exclusive), by step, where start defaults to 0 and step to 1." + ([end] (take end (iterate inc 0))) + ([start end] (take (- end start) (iterate inc start))) + ([start end step] (take-while (partial (if (pos? step) > <) end) (iterate (partial + step) start)))) -(defn - #^{:doc "Returns a map that consists of the rest of the maps - conj-ed onto the first. If a key occurs in more than one - map, the mapping from the latter (left-to-right) will be - the mapping in the result."} -merge [& maps] - (reduce conj maps)) - -(defn - #^{:doc "Returns a map that consists of the rest of the maps - conj-ed onto the first. If a key occurs in more than one - map, the mapping(s) from the latter (left-to-right) will - be combined with the mapping in the result by calling (f - val-in-result val-in-latter)."} -merge-with [f & maps] - (let [merge-entry (fn [m e] +(defn merge + "Returns a map that consists of the rest of the maps conj-ed onto + the first. If a key occurs in more than one map, the mapping from + the latter (left-to-right) will be the mapping in the result." + [& maps] (reduce conj maps)) + +(defn merge-with + "Returns a map that consists of the rest of the maps conj-ed onto + the first. If a key occurs in more than one map, the mapping(s) + from the latter (left-to-right) will be combined with the mapping in + the result by calling (f val-in-result val-in-latter)." + [f & maps] + (let [merge-entry (fn [m e] (let [k (key e) v (val e)] (if (contains? m k) (assoc m k (f (m k) v)) (assoc m k v)))) - merge2 (fn [m1 m2] + merge2 (fn [m1 m2] (reduce merge-entry m1 (seq m2)))] - (reduce merge2 maps))) + (reduce merge2 maps))) -(defn - #^{:doc "Returns a map with the keys mapped to the corresponding vals."} -zipmap [keys vals] - (loop [map {} - ks (seq keys) - vs (seq vals)] - (if (and ks vs) +(defn zipmap + "Returns a map with the keys mapped to the corresponding vals." + [keys vals] + (loop [map {} + ks (seq keys) + vs (seq vals)] + (if (and ks vs) (recur (assoc map (first ks) (first vs)) (rest ks) (rest vs)) - map))) - -(defn - #^{:doc "Returns the lines of text from rdr as a lazy sequence of - strings. rdr must implement java.io.BufferedReader."} -line-seq [#^java.io.BufferedReader rdr] - (let [line (. rdr (readLine))] - (when line - (lazy-cons line (line-seq rdr))))) - -(defn - #^{:doc "Returns an implementation of java.util.Comparator based - upon pred."} -comparator [pred] - (fn [x y] (cond (pred x y) -1 (pred y x) 1 :else 0))) + map))) + +(defn line-seq + "Returns the lines of text from rdr as a lazy sequence of strings. + rdr must implement java.io.BufferedReader." + [#^java.io.BufferedReader rdr] + (let [line (. rdr (readLine))] + (when line + (lazy-cons line (line-seq rdr))))) + +(defn comparator + "Returns an implementation of java.util.Comparator based upon pred." + [pred] + (fn [x y] + (cond (pred x y) -1 (pred y x) 1 :else 0))) -(defn - #^{:doc "Returns a sorted sequence of the items in coll. If no - comparator is supplied, the items must implement - Comparable. comparator must implement java.util.Comparator."} -sort +(defn sort + "Returns a sorted sequence of the items in coll. If no comparator is + supplied, the items must implement Comparable. comparator must + implement java.util.Comparator." ([#^java.util.Collection coll] (when (and coll (not (. coll (isEmpty)))) (let [a (. coll (toArray))] @@ -1154,33 +1145,30 @@ sort (. java.util.Arrays (sort a comp)) (seq a))))) -(defn - #^{:doc "Returns a sorted sequence of the items in coll, where the - sort order is determined by comparing (keyfn item). If no - comparator is supplied, the keys must implement - Comparable. comparator must implement java.util.Comparator."} -sort-by +(defn sort-by + "Returns a sorted sequence of the items in coll, where the sort + order is determined by comparing (keyfn item). If no comparator is + supplied, the keys must implement Comparable. comparator must + implement java.util.Comparator." ([keyfn coll] - (sort (fn [x y] (. #^Comparable (keyfn x) (compareTo (keyfn y)))) coll)) + (sort (fn [x y] (. #^Comparable (keyfn x) (compareTo (keyfn y)))) coll)) ([keyfn #^java.util.Comparator comp coll] - (sort (fn [x y] (. comp (compare (keyfn x) (keyfn y)))) coll))) + (sort (fn [x y] (. comp (compare (keyfn x) (keyfn y)))) coll))) ;; evaluation -(defn - #^{:doc "Evaluates the form data structure (not text!) and - returns the result."} -eval [form] - (. clojure.lang.Compiler (eval form))) +(defn eval + "Evaluates the form data structure (not text!) and returns the result." + [form] (. clojure.lang.Compiler (eval form))) ;(defn defimports [& imports-maps] ; (def *imports* (apply merge imports-maps))) -(defmacro - #^{:doc "Repeatedly executes body (presumably for side-effects) - with binding-form bound to successive items from coll. - Does not retain the head of the sequence. Returns nil."} -doseq [item list & body] +(defmacro doseq + "Repeatedly executes body (presumably for side-effects) with + binding-form bound to successive items from coll. Does not retain + the head of the sequence. Returns nil." + [item list & body] `(loop [list# (seq ~list)] (when list# (let [~item (first list#)] @@ -1190,30 +1178,26 @@ doseq [item list & body] (defn scan [& args] (throw (new Exception "scan is now called dorun"))) (defn touch [& args] (throw (new Exception "touch is now called doall"))) -(defn - #^{:doc "When lazy sequences are produced via functions that have - side effects, any effects other than those needed to - produce the first element in the seq do not occur until - the seq is consumed. dorun can be used to force any - effects. Walks through the successive rests of the seq, - does not retain the head and returns nil."} -dorun +(defn dorun + "When lazy sequences are produced via functions that have side + effects, any effects other than those needed to produce the first + element in the seq do not occur until the seq is consumed. dorun can + be used to force any effects. Walks through the successive rests of + the seq, does not retain the head and returns nil." ([coll] - (when (seq coll) - (recur (rest coll)))) + (when (seq coll) + (recur (rest coll)))) ([n coll] - (when (and (seq coll) (pos? n)) - (recur (dec n) (rest coll))))) - -(defn - #^{:doc "When lazy sequences are produced via functions that have - side effects, any effects other than those needed to - produce the first element in the seq do not occur until - the seq is consumed. doall can be used to force any - effects. Walks through the successive rests of the seq, - retains the head and returns it, thus causing the entire - seq to reside in memory at one time."} -doall + (when (and (seq coll) (pos? n)) + (recur (dec n) (rest coll))))) + +(defn doall + "When lazy sequences are produced via functions that have side + effects, any effects other than those needed to produce the first + element in the seq do not occur until the seq is consumed. doall can + be used to force any effects. Walks through the successive rests of + the seq, retains the head and returns it, thus causing the entire + seq to reside in memory at one time." ([coll] (dorun coll) coll) @@ -1221,72 +1205,71 @@ doall (dorun n coll) coll)) -(defn - #^{:doc "Blocks the current thread (indefinitely!) until all - actions dispatched thus far, from this thread or agent, to - the agent(s) have occurred."} -await [& agents] - (let [latch (new java.util.concurrent.CountDownLatch (count agents)) - count-down (fn [agent] (. latch (countDown)) agent)] - (doseq agent agents - (! agent count-down)) - (. latch (await)))) - -(defn - #^{:doc "Blocks the current thread until all actions dispatched - thus far (from this thread or agent) to the agents have - occurred, or the timeout (in milliseconds) has - elapsed. Returns nil if returning due to timeout, non-nil - otherwise."} -await-for [timeout-ms & agents] - (let [latch (new java.util.concurrent.CountDownLatch (count agents)) - count-down (fn [agent] (. latch (countDown)) agent)] - (doseq agent agents - (! agent count-down)) - (. latch (await timeout-ms (. java.util.concurrent.TimeUnit MILLISECONDS))))) +(defn await + "Blocks the current thread (indefinitely!) until all actions + dispatched thus far, from this thread or agent, to the agent(s) have + occurred." + [& agents] + (let [latch (new java.util.concurrent.CountDownLatch (count agents)) + count-down (fn [agent] (. latch (countDown)) agent)] + (doseq agent agents + (! agent count-down)) + (. latch (await)))) + +(defn await-for + "Blocks the current thread until all actions dispatched thus + far (from this thread or agent) to the agents have occurred, or the + timeout (in milliseconds) has elapsed. Returns nil if returning due + to timeout, non-nil otherwise." + [timeout-ms & agents] + (let [latch (new java.util.concurrent.CountDownLatch (count agents)) + count-down (fn [agent] (. latch (countDown)) agent)] + (doseq agent agents + (! agent count-down)) + (. latch (await timeout-ms (. java.util.concurrent.TimeUnit MILLISECONDS))))) -(defmacro - #^{:doc "Repeatedly executes body (presumably for side-effects) with - name bound to integers from 0 through n-1."} -dotimes [i n & body] +(defmacro dotimes + "Repeatedly executes body (presumably for side-effects) with name + bound to integers from 0 through n-1." + [i n & body] `(loop [~i 0 n# ~n] (when (< ~i n#) ~@body (recur (inc ~i) n#)))) -(defn - #^{:doc "import-list => (package-symbol class-name-symbols*) - For each name in class-name-symbols, adds a mapping from - name to the class named by package.name to the current - namespace."} -import [& import-lists] - (when import-lists - (let [#^clojure.lang.Namespace ns *ns* - pkg (ffirst import-lists) - classes (rfirst import-lists)] - (doseq c classes - (. ns (importClass c (. Class (forName (str pkg "." c)))))) ) - (apply import (rest import-lists)))) - -(defn - #^{:doc "Returns an array of the type of the first element in - coll, containing the contents of coll, which must be of a - compatible type."} -into-array [aseq] - (. clojure.lang.RT (seqToTypedArray (seq aseq)))) - -(defn - #^{:doc "Returns a new coll consisting of to-coll with all - of the items of from-coll conjoined."} -into [to from] - (let [ret to items (seq from)] - (if items - (recur (conj ret (first items)) (rest items)) - ret))) +(defn import + "import-list => (package-symbol class-name-symbols*) + + For each name in class-name-symbols, adds a mapping from name to the + class named by package.name to the current namespace." + [& import-lists] + (when import-lists + (let [#^clojure.lang.Namespace ns *ns* + pkg (ffirst import-lists) + classes (rfirst import-lists)] + (doseq c classes + (. ns (importClass c (. Class (forName (str pkg "." c)))))) ) + (apply import (rest import-lists)))) + +(defn into-array + "Returns an array of the type of the first element in coll, + containing the contents of coll, which must be of a compatible + type." + [aseq] + (. clojure.lang.RT (seqToTypedArray (seq aseq)))) + +(defn into + "Returns a new coll consisting of to-coll with all of the items of + from-coll conjoined." + [to from] + (let [ret to items (seq from)] + (if items + (recur (conj ret (first items)) (rest items)) + ret))) (defn #^{:private true} -array [& items] - (into-array items)) + array [& items] + (into-array items)) (defn make-proxy [classes method-map] @@ -1295,32 +1278,32 @@ make-proxy [classes method-map] (into-array classes) (new clojure.lang.ProxyHandler method-map)))) -(defmacro - #^{:doc "f => (name [args+] body) - Expands to code which creates a instance of a class that - implements the named interface(s) by calling the supplied - fns. The interface names must be valid class names of - interface types. If a method is not provided for a - non-void-returning interface method, an - UnsupportedOperationException will be thrown should it be - called. Method fns are closures and can capture the - environment in which implement is called. "} -implement [interfaces & fs] +(defmacro implement + "f => (name [args+] body) + + Deprecated - use proxy. + Expands to code which creates a instance of a class that implements + the named interface(s) by calling the supplied fns. The interface + names must be valid class names of interface types. If a method is + not provided for a non-void-returning interface method, an + UnsupportedOperationException will be thrown should it be called. + Method fns are closures and can capture the environment in which + implement is called." + [interfaces & fs] `(make-proxy - ~interfaces - ~(loop [fmap {} fs fs] - (if fs - (recur (assoc fmap (name (ffirst fs)) - (cons `fn (rfirst fs))) - (rest fs)) - fmap)))) - -(defn - #^{:doc "Prints the object(s) to the output stream that is the - current value of *out*. Prints the object(s), separated - by spaces if there is more than one. By default, pr and - prn print in a way that objects can be read by the reader"} -pr + ~interfaces + ~(loop [fmap {} fs fs] + (if fs + (recur (assoc fmap (name (ffirst fs)) + (cons `fn (rfirst fs))) + (rest fs)) + fmap)))) + +(defn pr + "Prints the object(s) to the output stream that is the current value + of *out*. Prints the object(s), separated by spaces if there is + more than one. By default, pr and prn print in a way that objects + can be read by the reader" ([] nil) ([x] (. clojure.lang.RT (print x *out*)) @@ -1330,145 +1313,145 @@ pr (. *out* (append \space)) (apply pr more))) -(defn - #^{:doc "Writes a newline to the output stream that is - the current value of *out*"} -newline [] - (. *out* (append \newline)) - nil) - -(defn - #^{:doc "Same as pr followed by (newline)"} -prn [& more] - (apply pr more) - (newline)) - -(defn - #^{:doc "Prints the object(s) to the output stream that is the - current value of *out*. print and println produce output - for human consumption."} -print [& more] - (binding [*print-readably* nil] - (apply pr more))) - -(defn - #^{:doc "Same as print followed by (newline)"} -println [& more] - (binding [*print-readably* nil] - (apply prn more))) - - -(defn - #^{:doc "Reads the next object from stream, which must be an - instance of java.io.PushbackReader or some derivee. - stream defaults to the current value of *in* ."} -read +(defn newline + "Writes a newline to the output stream that is the current value of + *out*" + [] + (. *out* (append \newline)) + nil) + +(defn prn + "Same as pr followed by (newline)" + [& more] + (apply pr more) + (newline)) + +(defn print + "Prints the object(s) to the output stream that is the current value + of *out*. print and println produce output for human consumption." + [& more] + (binding [*print-readably* nil] + (apply pr more))) + +(defn println + "Same as print followed by (newline)" + [& more] + (binding [*print-readably* nil] + (apply prn more))) + + +(defn read + "Reads the next object from stream, which must be an instance of + java.io.PushbackReader or some derivee. stream defaults to the + current value of *in* ." ([] - (read *in*)) + (read *in*)) ([stream] - (read stream true nil)) + (read stream true nil)) ([stream eof-error? eof-value] - (read stream eof-error? eof-value false)) + (read stream eof-error? eof-value false)) ([stream eof-error? eof-value recursive?] - (. clojure.lang.LispReader (read stream eof-error? eof-value recursive?)))) + (. clojure.lang.LispReader (read stream eof-error? eof-value recursive?)))) -(defmacro - #^{:doc "Evaluates body in a try expression with name bound to the - value of init, and a finally clause that calls (. name - (close))."} -with-open [name init & body] +(defmacro with-open + "Evaluates body in a try expression with name bound to the value of + init, and a finally clause that calls (. name (close))." + [name init & body] `(let [~name ~init] (try ~@body (finally - (. ~name (close)))))) - -(defmacro - #^{:doc "Evaluates x then calls all of the methods with the - supplied arguments in succession on the resulting object, - returning it. - - (doto (new java.util.HashMap) (put \"a\" 1) (put \"b\" 2))"} -doto [x & members] - (let [gx (gensym)] - `(let [~gx ~x] - (do - ~@(map (fn [m] (list '. gx m)) - members)) - ~gx))) - -(defmacro - #^{:doc "Expands into code that creates a fn that expects to be - passed an object and any args and calls the named instance - method on the object passing the args. Use when you want - to treat a Java method as a first-class fn."} -memfn [name & args] + (. ~name (close)))))) + +(defmacro doto + "Evaluates x then calls all of the methods with the supplied + arguments in succession on the resulting object, returning it. + + (doto (new java.util.HashMap) (put \"a\" 1) (put \"b\" 2))" + [x & members] + (let [gx (gensym)] + `(let [~gx ~x] + (do + ~@(map (fn [m] (list '. gx m)) + members)) + ~gx))) + +(defmacro memfn + "Expands into code that creates a fn that expects to be passed an + object and any args and calls the named instance method on the + object passing the args. Use when you want to treat a Java method as + a first-class fn." + [name & args] `(fn [target# ~@args] - (. target# (~name ~@args)))) - -(defmacro - #^{:doc "Evaluates expr and prints the time it took. - Returns the value of expr."} -time [expr] - `(let [start# (. System (nanoTime)) - ret# ~expr] - (prn (str "Elapsed time: " (/ (- (. System (nanoTime)) start#) 1000000.0) " msecs")) - ret#)) - - -(defn #^{:tag Integer :doc "Coerce to int"} -int [x] - (. clojure.lang.RT (intCast x))) - -(defn #^{:tag Long :doc "Coerce to long"} -long [#^Number x] - (. x (longValue))) - -(defn #^{:tag Float :doc "Coerce to float"} -float [#^Number x] - (. x (floatValue))) - -(defn #^{:tag Double :doc "Coerce to double"} -double [#^Number x] - (. x (doubleValue))) - -(defn #^{:tag Short :doc "Coerce to short"} -short [#^Number x] - (. x (shortValue))) - -(defn #^{:tag Byte :doc "Coerce to byte"} -byte [#^Number x] - (. x (byteValue))) - -(defn #^{:tag Character :doc "Coerce to char"} -char [x] - (. clojure.lang.RT (charCast x))) - -(defn #^{:tag Boolean :doc "Coerce to boolean"} -boolean [x] - (if x true false)) + (. target# (~name ~@args)))) + +(defmacro time + "Evaluates expr and prints the time it took. Returns the value of + expr." + [expr] + `(let [start# (. System (nanoTime)) + ret# ~expr] + (prn (str "Elapsed time: " (/ (- (. System (nanoTime)) start#) 1000000.0) " msecs")) + ret#)) + +(defn int + "Coerce to int" + {:tag Integer} + [x] (. clojure.lang.RT (intCast x))) + +(defn long + "Coerce to long" + {:tag Long} + [#^Number x] (. x (longValue))) + +(defn float + "Coerce to float" + {:tag Float} + [#^Number x] (. x (floatValue))) + +(defn double + "Coerce to double" + {:tag Double} + [#^Number x] (. x (doubleValue))) + +(defn short + "Coerce to short" + {:tag Short} + [#^Number x] (. x (shortValue))) + +(defn byte + "Coerce to byte" + {:tag Byte} + [#^Number x] (. x (byteValue))) + +(defn char + "Coerce to char" + {:tag Character} + [x] (. clojure.lang.RT (charCast x))) + +(defn boolean + "Coerce to boolean" + {:tag Boolean} + [x] (if x true false)) (import '(java.lang.reflect Array)) -(defn - #^{:doc "Returns the length of the Java array. Works on arrays - of all types."} -alength [array] - (. Array (getLength array))) +(defn alength + "Returns the length of the Java array. Works on arrays of all + types." + [array] (. Array (getLength array))) -(defn - #^{:doc "Returns the value at the index/indices. Works on Java arrays - of all types."} -aget +(defn aget + "Returns the value at the index/indices. Works on Java arrays of all + types." ([array idx] (. Array (get array idx))) ([array idx & idxs] (apply aget (aget array idx) idxs))) -(defn - #^{:doc "Sets the value at the index/indices. Works on Java arrays - of reference types. Returns val."} -aset +(defn aset + "Sets the value at the index/indices. Works on Java arrays of + reference types. Returns val." ([array idx val] (. Array (set array idx val)) val) @@ -1476,85 +1459,81 @@ aset (apply aset (aget array idx) idx2 idxv))) (defmacro -#^{:private true} -def-aset [name method coerce] - `(defn ~name - ([array# idx# val#] - (. Array (~method array# idx# (~coerce val#))) - val#) - ([array# idx# idx2# & idxv#] - (apply ~name (aget array# idx#) idx2# idxv#)))) + #^{:private true} + def-aset [name method coerce] + `(defn ~name + ([array# idx# val#] + (. Array (~method array# idx# (~coerce val#))) + val#) + ([array# idx# idx2# & idxv#] + (apply ~name (aget array# idx#) idx2# idxv#)))) (def-aset - #^{:doc "Sets the value at the index/indices. Works on arrays of int. - Returns val."} -aset-int setInt int) -(def-aset - #^{:doc "Sets the value at the index/indices. Works on arrays of long. - Returns val."} -aset-long setLong long) + #^{:doc "Sets the value at the index/indices. Works on arrays of int. Returns val."} + aset-int setInt int) + (def-aset - #^{:doc "Sets the value at the index/indices. Works on arrays of boolean. - Returns val."} -aset-boolean setBoolean boolean) + #^{:doc "Sets the value at the index/indices. Works on arrays of long. Returns val."} + aset-long setLong long) + (def-aset - #^{:doc "Sets the value at the index/indices. Works on arrays of float. - Returns val."} -aset-float setFloat float) + #^{:doc "Sets the value at the index/indices. Works on arrays of boolean. Returns val."} + aset-boolean setBoolean boolean) + (def-aset - #^{:doc "Sets the value at the index/indices. Works on arrays of double. - Returns val."} -aset-double setDouble double) + #^{:doc "Sets the value at the index/indices. Works on arrays of float. Returns val."} + aset-float setFloat float) + (def-aset - #^{:doc "Sets the value at the index/indices. Works on arrays of short. - Returns val."} -aset-short setShort short) + #^{:doc "Sets the value at the index/indices. Works on arrays of double. Returns val."} + aset-double setDouble double) + (def-aset - #^{:doc "Sets the value at the index/indices. Works on arrays of byte. - Returns val."} -aset-byte setByte byte) + #^{:doc "Sets the value at the index/indices. Works on arrays of short. Returns val."} + aset-short setShort short) + (def-aset - #^{:doc "Sets the value at the index/indices. Works on arrays of char. - Returns val."} -aset-char setChar char) + #^{:doc "Sets the value at the index/indices. Works on arrays of byte. Returns val."} + aset-byte setByte byte) -(defn - #^{:doc "Creates and returns an array of instances of the - specified class of the specified dimension(s). Note that - a class object is required. - Class objects can be obtained by using their imported or - fully-qualified name. Class objects for the primitive - types can be obtained using, e.g., (. Integer TYPE)."} -make-array +(def-aset + #^{:doc "Sets the value at the index/indices. Works on arrays of char. Returns val."} + aset-char setChar char) + +(defn make-array + "Creates and returns an array of instances of the specified class of + the specified dimension(s). Note that a class object is required. + Class objects can be obtained by using their imported or + fully-qualified name. Class objects for the primitive types can be + obtained using, e.g., (. Integer TYPE)." ([#^Class type len] - (. Array (newInstance type (int len)))) + (. Array (newInstance type (int len)))) ([#^Class type dim & more-dims] - (let [dims (cons dim more-dims) - #^"[I" dimarray (make-array (. Integer TYPE) (count dims))] - (dotimes i (alength dimarray) - (aset-int dimarray i (nth dims i))) - (. Array (newInstance type dimarray))))) - -(defn - #^{:doc "Returns an array of Objects containing the contents of - coll, which can be any Collection. Maps to - java.util.Collection.toArray()."} -to-array [#^java.util.Collection coll] - (if (zero? (count coll)) - (. clojure.lang.RT EMPTY_ARRAY) - (. coll (toArray)))) - -(defn - #^{:doc "Returns a (potentially-ragged) 2-dimensional array of - Objects containing the contents of coll, which can be any - Collection of any Collection."} -to-array-2d [#^java.util.Collection coll] - (let [ret (make-array (. Class (forName "[Ljava.lang.Object;")) (. coll (size)))] - (loop [i 0 xs (seq coll)] - (when xs - (aset ret i (to-array (first xs))) - (recur (inc i) (rest xs)))) - ret)) + (let [dims (cons dim more-dims) + #^"[I" dimarray (make-array (. Integer TYPE) (count dims))] + (dotimes i (alength dimarray) + (aset-int dimarray i (nth dims i))) + (. Array (newInstance type dimarray))))) + +(defn to-array + "Returns an array of Objects containing the contents of coll, which + can be any Collection. Maps to java.util.Collection.toArray()." + [#^java.util.Collection coll] + (if (zero? (count coll)) + (. clojure.lang.RT EMPTY_ARRAY) + (. coll (toArray)))) + +(defn to-array-2d + "Returns a (potentially-ragged) 2-dimensional array of Objects + containing the contents of coll, which can be any Collection of any + Collection." + [#^java.util.Collection coll] + (let [ret (make-array (. Class (forName "[Ljava.lang.Object;")) (. coll (size)))] + (loop [i 0 xs (seq coll)] + (when xs + (aset ret i (to-array (first xs))) + (recur (inc i) (rest xs)))) + ret)) (import '(java.util.concurrent Executors LinkedBlockingQueue)) @@ -1595,245 +1574,231 @@ to-array-2d [#^java.util.Collection coll] (thisfn (map rest collseq)))))] (encl-fn (cons coll colls)))))) -(defn - #^{:doc "If form represents a macro form, returns its expansion, - else returns form."} -macroexpand-1 [form] - (let [v (. clojure.lang.Compiler (isMacro (first form)))] - (if v - (apply @v (rest form)) - form))) - -(defn - #^{:doc "Repeatedly calls macroexpand-1 on form until it no longer - represents a macro form, then returns it. Note neither - macroexpand-1 nor macroexpand expand macros in subforms."} -macroexpand [form] - (let [ex (macroexpand-1 form) - v (. clojure.lang.Compiler (isMacro (first ex)))] - (if v - (macroexpand ex) - ex))) - -(defn - #^{:doc "Returns a structure basis object."} -create-struct [& keys] - (. clojure.lang.PersistentStructMap (createSlotMap keys))) - -(defmacro - #^{:doc "Same as (def name (create-struct keys...))"} -defstruct [name & keys] +(defn macroexpand-1 + "If form represents a macro form, returns its expansion, + else returns form." + [form] + (let [v (. clojure.lang.Compiler (isMacro (first form)))] + (if v + (apply @v (rest form)) + form))) + +(defn macroexpand + "Repeatedly calls macroexpand-1 on form until it no longer + represents a macro form, then returns it. Note neither + macroexpand-1 nor macroexpand expand macros in subforms." + [form] + (let [ex (macroexpand-1 form) + v (. clojure.lang.Compiler (isMacro (first ex)))] + (if v + (macroexpand ex) + ex))) + +(defn create-struct + "Returns a structure basis object." + [& keys] + (. clojure.lang.PersistentStructMap (createSlotMap keys))) + +(defmacro defstruct + "Same as (def name (create-struct keys...))" + [name & keys] `(def ~name (create-struct ~@keys))) -(defn - #^{:doc "Returns a new structmap instance with the keys of the - structure-basis. keyvals may contain all, some or none of - the basis keys - where values are not supplied they will - default to nil. keyvals can also contain keys not in the - basis."} -struct-map [s & inits] - (. clojure.lang.PersistentStructMap (create s inits))) - -(defn - #^{:doc "Returns a new structmap instance with the keys of the - structure-basis. vals must be supplied for basis keys in - order - where values are not supplied they will default to - nil."} -struct [s & vals] - (. clojure.lang.PersistentStructMap (construct s vals))) - -(defn - #^{:doc "Returns a fn that, given an instance of a structmap with - the basis, returns the value at the key. The key must be - in the basis. The returned function should be (slightly) - more efficient than using get, but such use of accessors - should be limited to known performance-critical areas."} -accessor [s key] - (. clojure.lang.PersistentStructMap (getAccessor s key))) - -(defn - #^{:doc "Returns a persistent vector of the items in vector from - start (inclusive) to end (exclusive). If end is not - supplied, defaults to (count vector). This operation is - O(1) and very fast, as the resulting vector shares - structure with the original and no trimming is done."} -subvec +(defn struct-map + "Returns a new structmap instance with the keys of the + structure-basis. keyvals may contain all, some or none of the basis + keys - where values are not supplied they will default to nil. + keyvals can also contain keys not in the basis." + [s & inits] + (. clojure.lang.PersistentStructMap (create s inits))) + +(defn struct + "Returns a new structmap instance with the keys of the + structure-basis. vals must be supplied for basis keys in order - + where values are not supplied they will default to nil." + [s & vals] + (. clojure.lang.PersistentStructMap (construct s vals))) + +(defn accessor + "Returns a fn that, given an instance of a structmap with the basis, + returns the value at the key. The key must be in the basis. The + returned function should be (slightly) more efficient than using + get, but such use of accessors should be limited to known + performance-critical areas." + [s key] + (. clojure.lang.PersistentStructMap (getAccessor s key))) + +(defn subvec + "Returns a persistent vector of the items in vector from + start (inclusive) to end (exclusive). If end is not supplied, + defaults to (count vector). This operation is O(1) and very fast, as + the resulting vector shares structure with the original and no + trimming is done." ([v start] - (subvec v start (count v))) + (subvec v start (count v))) ([v start end] - (. clojure.lang.RT (subvec v start end)))) - -(defn - #^{:doc "sequentially read and evaluate the set of forms contained - in the stream/file"} -load [rdr] - (. clojure.lang.Compiler (load rdr))) - -(defn - #^{:doc "Creates and returns a lazy sequence of structmaps corresponding - to the rows in the java.sql.ResultSet rs"} -resultset-seq [#^java.sql.ResultSet rs] - (let [rsmeta (. rs (getMetaData)) - idxs (range 1 (inc (. rsmeta (getColumnCount)))) - keys (map (comp keyword (memfn toLowerCase)) - (map (fn [i] (. rsmeta (getColumnName i))) idxs)) - row-struct (apply create-struct keys) - row-values (fn [] (map (fn [#^Integer i] (. rs (getObject i))) idxs)) - rows (fn thisfn [] - (when (. rs (next)) + (. clojure.lang.RT (subvec v start end)))) + +(defn load + "Sequentially read and evaluate the set of forms contained in the + stream/file" + [rdr] (. clojure.lang.Compiler (load rdr))) + +(defn resultset-seq + "Creates and returns a lazy sequence of structmaps corresponding to + the rows in the java.sql.ResultSet rs" + [#^java.sql.ResultSet rs] + (let [rsmeta (. rs (getMetaData)) + idxs (range 1 (inc (. rsmeta (getColumnCount)))) + keys (map (comp keyword (memfn toLowerCase)) + (map (fn [i] (. rsmeta (getColumnName i))) idxs)) + row-struct (apply create-struct keys) + row-values (fn [] (map (fn [#^Integer i] (. rs (getObject i))) idxs)) + rows (fn thisfn [] + (when (. rs (next)) (fnseq (apply struct row-struct (row-values)) thisfn)))] - (rows))) + (rows))) -(defn - #^{:doc "Returns a map of the distinct elements of coll to true."} -set [coll] - (apply hash-set coll)) +(defn set + "Returns a set of the distinct elements of coll." + [coll] (apply hash-set coll)) -(defn - #^{:doc "Returns a sequence of the elements of coll with - duplicates removed"} -distinct [coll] - (seq (set coll))) +(defn distinct + "Returns a sequence of the elements of coll with duplicates removed" + [coll] (seq (set coll))) (defn #^{:private true} -filter-key [keyfn pred amap] |