Mercurial > lasercutter
view src/clojure/contrib/core.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 source
1 ; Copyright (c) Laurent Petit and others, March 2009. All rights reserved.3 ; The use and distribution terms for this software are covered by the4 ; 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 this6 ; distribution.7 ; By using this software in any fashion, you are agreeing to be bound by8 ; the terms of this license.9 ; You must not remove this notice, or any other, from this software.11 ;; functions/macros variants of the ones that can be found in clojure.core13 ;; note to other contrib members: feel free to add to this lib15 (ns16 ^{:author "Laurent Petit (and others)"17 :doc "Functions/macros variants of the ones that can be found in clojure.core18 (note to other contrib members: feel free to add to this lib)"}19 clojure.contrib.core20 (:use clojure.contrib.def))22 (defmacro- defnilsafe [docstring non-safe-name nil-safe-name]23 `(defmacro ~nil-safe-name ~docstring24 {: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#))))30 (defnilsafe31 "Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).32 Examples :33 (-?> \"foo\" .toUpperCase (.substring 1)) returns \"OO\"34 (-?> nil .toUpperCase (.substring 1)) returns nil35 "36 -> -?>)38 (defnilsafe39 "Same as clojure.core/.. but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).40 Examples :41 (.?. \"foo\" .toUpperCase (.substring 1)) returns \"OO\"42 (.?. nil .toUpperCase (.substring 1)) returns nil43 "44 .. .?.)46 (defnilsafe47 "Same as clojure.core/->> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).48 Examples :49 (-?>> (range 5) (map inc)) returns (1 2 3 4 5)50 (-?>> [] seq (map inc)) returns nil51 "52 ->> -?>>)54 ;; ----------------------------------------------------------------------55 ;; scgilardi at gmail57 (defn dissoc-in58 "Dissociates an entry from a nested associative structure returning a new59 nested structure. keys is a sequence of keys. Any empty maps that result60 will not be present in the new structure."61 [m [k & ks :as keys]]62 (if ks63 (if-let [nextmap (get m k)]64 (let [newmap (dissoc-in nextmap ks)]65 (if (seq newmap)66 (assoc m k newmap)67 (dissoc m k)))68 m)69 (dissoc m k)))71 (defn new-by-name72 "Constructs a Java object whose class is specified by a String."73 [class-name & args]74 (clojure.lang.Reflector/invokeConstructor75 (clojure.lang.RT/classForName class-name)76 (into-array Object args)))78 (defn seqable?79 "Returns true if (seq x) will succeed, false otherwise."80 [x]81 (or (seq? x)82 (instance? clojure.lang.Seqable x)83 (nil? x)84 (instance? Iterable x)85 (-> x .getClass .isArray)86 (string? x)87 (instance? java.util.Map x)))89 ;; ----------------------------------------------------------------------