Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pulling in c.c.core and defmacro- from c.c.def
  • Loading branch information
abedra committed May 4, 2011
1 parent b1ef0fd commit 33b04a0
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
target
18 changes: 18 additions & 0 deletions pom.xml
@@ -0,0 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core.incubator</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>${artifactId}</name>

<parent>
<groupId>org.clojure</groupId>
<artifactId>pom.contrib</artifactId>
<version>0.0.20</version>
</parent>

<scm>
<connection>scm:git:git@github.com:clojure/core.incubator.git</connection>
<developerConnection>scm:git:git@github.com:clojure/core.incubator.git</developerConnection>
<url>git@github.com:clojure/core.incubator.git</url>
</scm>
</project>
86 changes: 86 additions & 0 deletions src/main/clojure/clojure/core/incubator.clj
@@ -0,0 +1,86 @@
; Copyright (c) Laurent Petit and others, March 2009. All rights reserved.

; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns ^{:author "Laurent Petit (and others)"
:doc "Functions/macros variants of the ones that can be found in clojure.core
(note to other contrib members: feel free to add to this lib)"}
clojure.core.incubator)

(defmacro defmacro-
"Same as defmacro but yields a private definition"
[name & decls]
(list* `defmacro (with-meta name (assoc (meta name) :private true)) decls))

(defmacro- defnilsafe [docstring non-safe-name nil-safe-name]
`(defmacro ~nil-safe-name ~docstring
{:arglists '([~'x ~'form] [~'x ~'form ~'& ~'forms])}
([x# form#]
`(let [~'i# ~x#] (when-not (nil? ~'i#) (~'~non-safe-name ~'i# ~form#))))
([x# form# & more#]
`(~'~nil-safe-name (~'~nil-safe-name ~x# ~form#) ~@more#))))

(defnilsafe
"Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
Examples :
(-?> \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
(-?> nil .toUpperCase (.substring 1)) returns nil
"
-> -?>)

(defnilsafe
"Same as clojure.core/.. but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
Examples :
(.?. \"foo\" .toUpperCase (.substring 1)) returns \"OO\"
(.?. nil .toUpperCase (.substring 1)) returns nil
"
.. .?.)

(defnilsafe
"Same as clojure.core/->> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
Examples :
(-?>> (range 5) (map inc)) returns (1 2 3 4 5)
(-?>> [] seq (map inc)) returns nil
"
->> -?>>)

;; ----------------------------------------------------------------------
;; scgilardi at gmail

(defn dissoc-in
"Dissociates an entry from a nested associative structure returning a new
nested structure. keys is a sequence of keys. Any empty maps that result
will not be present in the new structure."
[m [k & ks :as keys]]
(if ks
(if-let [nextmap (get m k)]
(let [newmap (dissoc-in nextmap ks)]
(if (seq newmap)
(assoc m k newmap)
(dissoc m k)))
m)
(dissoc m k)))

(defn new-by-name
"Constructs a Java object whose class is specified by a String."
[class-name & args]
(clojure.lang.Reflector/invokeConstructor
(clojure.lang.RT/classForName class-name)
(into-array Object args)))

(defn seqable?
"Returns true if (seq x) will succeed, false otherwise."
[x]
(or (seq? x)
(instance? clojure.lang.Seqable x)
(nil? x)
(instance? Iterable x)
(-> x .getClass .isArray)
(string? x)
(instance? java.util.Map x)))
37 changes: 37 additions & 0 deletions src/test/clojure/clojure/core/incubator.clj
@@ -0,0 +1,37 @@
; Copyright (c) Laurent Petit, March 2009. All rights reserved.

; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns clojure.contrib.test-core
(:use clojure.test
clojure.core.incubator))

(deftest test-classic-versions
(testing "Classic -> throws NPE if passed nil"
(is (thrown? NullPointerException (-> nil .toString)))
(is (thrown? NullPointerException (-> "foo" seq next next next .toString))))
(testing "Classic .. throws NPE if one of the intermediate threaded values is nil"
(is (thrown? NullPointerException (.. nil toString)))
(is (thrown? NullPointerException (.. [nil] (get 0) toString)))))

(deftest test-new-versions
(testing "Version -?>> falls out on nil"
(is (nil? (-?>> nil .toString)))
(is (nil? (-?>> [] seq (map inc))))
(is (= [] (->> [] seq (map inc)))))
(testing "Version -?>> completes for non-nil"
(is (= [3 4] (-?>> [1 2] (map inc) (map inc)))))
(testing "Version -?> falls out on nil"
(is (nil? (-?> nil .toString)))
(is (nil? (-?> "foo" seq next next next .toString))))
(testing "Version -?> completes for non-nil"
(is (= [\O \O] (-?> "foo" .toUpperCase rest))))
(testing "Version .?. returns nil if one of the intermediate threaded values is nil"
(is (nil? (.?. nil toString)))
(is (nil? (.?. [nil] (get 0) toString)))))

0 comments on commit 33b04a0

Please sign in to comment.