diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/core.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,89 @@
     1.4 +;   Copyright (c) Laurent Petit and others, March 2009. All rights reserved.
     1.5 +
     1.6 +;   The use and distribution terms for this software are covered by the
     1.7 +;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.8 +;   which can be found in the file epl-v10.html at the root of this 
     1.9 +;   distribution.
    1.10 +;   By using this software in any fashion, you are agreeing to be bound by
    1.11 +;   the terms of this license.
    1.12 +;   You must not remove this notice, or any other, from this software.
    1.13 +
    1.14 +;; functions/macros variants of the ones that can be found in clojure.core
    1.15 +
    1.16 +;; note to other contrib members: feel free to add to this lib
    1.17 +
    1.18 +(ns
    1.19 +  ^{:author "Laurent Petit (and others)"
    1.20 +     :doc "Functions/macros variants of the ones that can be found in clojure.core 
    1.21 + (note to other contrib members: feel free to add to this lib)"}
    1.22 +  clojure.contrib.core
    1.23 +  (:use clojure.contrib.def))
    1.24 +
    1.25 +(defmacro- defnilsafe [docstring non-safe-name nil-safe-name]
    1.26 +  `(defmacro ~nil-safe-name ~docstring
    1.27 +     {:arglists '([~'x ~'form] [~'x ~'form ~'& ~'forms])}
    1.28 +	   ([x# form#]
    1.29 +	     `(let [~'i# ~x#] (when-not (nil? ~'i#) (~'~non-safe-name ~'i# ~form#))))
    1.30 +  	 ([x# form# & more#]
    1.31 +	     `(~'~nil-safe-name (~'~nil-safe-name ~x# ~form#) ~@more#))))
    1.32 +       
    1.33 +(defnilsafe 
    1.34 +  "Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
    1.35 +   Examples :
    1.36 +   (-?> \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
    1.37 +   (-?> nil .toUpperCase (.substring 1)) returns nil
    1.38 +   "
    1.39 +   -> -?>)
    1.40 +    
    1.41 +(defnilsafe 
    1.42 +  "Same as clojure.core/.. but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
    1.43 +   Examples :
    1.44 +   (.?. \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
    1.45 +   (.?. nil .toUpperCase (.substring 1)) returns nil
    1.46 +   "
    1.47 +   .. .?.)
    1.48 +
    1.49 +(defnilsafe
    1.50 +  "Same as clojure.core/->> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
    1.51 +   Examples :
    1.52 +   (-?>> (range 5) (map inc)) returns (1 2 3 4 5)
    1.53 +   (-?>> [] seq (map inc)) returns nil
    1.54 +   "
    1.55 +  ->> -?>>)
    1.56 +
    1.57 +;; ----------------------------------------------------------------------
    1.58 +;; scgilardi at gmail
    1.59 +
    1.60 +(defn dissoc-in
    1.61 +  "Dissociates an entry from a nested associative structure returning a new
    1.62 +  nested structure. keys is a sequence of keys. Any empty maps that result
    1.63 +  will not be present in the new structure."
    1.64 +  [m [k & ks :as keys]]
    1.65 +  (if ks
    1.66 +    (if-let [nextmap (get m k)]
    1.67 +      (let [newmap (dissoc-in nextmap ks)]
    1.68 +        (if (seq newmap)
    1.69 +          (assoc m k newmap)
    1.70 +          (dissoc m k)))
    1.71 +      m)
    1.72 +    (dissoc m k)))
    1.73 +
    1.74 +(defn new-by-name
    1.75 +  "Constructs a Java object whose class is specified by a String."
    1.76 +  [class-name & args]
    1.77 +  (clojure.lang.Reflector/invokeConstructor
    1.78 +   (clojure.lang.RT/classForName class-name)
    1.79 +   (into-array Object args)))
    1.80 +
    1.81 +(defn seqable?
    1.82 +  "Returns true if (seq x) will succeed, false otherwise."
    1.83 +  [x]
    1.84 +  (or (seq? x)
    1.85 +      (instance? clojure.lang.Seqable x)
    1.86 +      (nil? x)
    1.87 +      (instance? Iterable x)
    1.88 +      (-> x .getClass .isArray)
    1.89 +      (string? x)
    1.90 +      (instance? java.util.Map x)))
    1.91 +
    1.92 +;; ----------------------------------------------------------------------