diff options
author | Rich Hickey <richhickey@gmail.com> | 2009-02-14 15:16:28 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2009-02-14 15:16:28 +0000 |
commit | 8c59f860762c7b202f0926be401a19e031bfa110 (patch) | |
tree | 067102f71a73a4615ef562b89da9673d4e8c44da | |
parent | db2710113b7019e6d28847b7cb598ace9ee923b9 (diff) |
[lazy] added (optional) detection of conditional test of LazySeq, build with:
ant -Dclojure.assert-if-lazy-seq=true
patch from Chouser
-rw-r--r-- | build.xml | 2 | ||||
-rw-r--r-- | src/clj/clojure/core.clj | 20 | ||||
-rw-r--r-- | src/clj/clojure/core_proxy.clj | 2 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Compiler.java | 2 |
4 files changed, 23 insertions, 3 deletions
@@ -11,6 +11,7 @@ <property name="build" location="classes"/> <property name="clojure_jar" location="clojure.jar"/> <property name="slim_jar" location="clojure-slim.jar"/> + <property name="clojure.assert-if-lazy-seq" value=""/> <target name="init" depends="clean"> <tstamp/> @@ -28,6 +29,7 @@ <java classname="clojure.lang.Compile" classpath="${build}:${cljsrc}"> <sysproperty key="clojure.compile.path" value="${build}"/> + <sysproperty key="clojure.assert-if-lazy-seq" value="${clojure.assert-if-lazy-seq}"/> <arg value="clojure.core"/> <arg value="clojure.main"/> <arg value="clojure.set"/> diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index eb310055..3cdeb73c 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -36,6 +36,10 @@ fn (fn* fn [& decl] (cons 'fn* decl))) (def + #^{:macro true} + if (fn* if [& decl] (cons 'if* decl))) + +(def #^{:arglists '([coll]) :doc "Returns the first item in the collection. Calls seq on its argument. If coll is nil, returns nil."} @@ -291,6 +295,20 @@ (. (var defmacro) (setMacro)) +(defmacro assert-if-lazy-seq? {:private true} [] + (let [prop (System/getProperty "clojure.assert-if-lazy-seq")] + (if prop + (if (clojure.lang.Util/equals prop "") nil true)))) + +(defmacro if [tst & etc] + (if* (assert-if-lazy-seq?) + (let [tstsym 'G__0_0] + (list 'let [tstsym tst] + (list 'if* (list 'clojure.core/instance? clojure.lang.LazySeq tstsym) + (list 'throw (list 'new Exception "LazySeq used in 'if'")) + (cons 'if* (cons tstsym etc))))) + (cons 'if* (cons tst etc)))) + (defmacro when "Evaluates test. If logical true, evaluates body in an implicit do." [test & body] @@ -1642,7 +1660,7 @@ ([coll] (sort compare coll)) ([#^java.util.Comparator comp coll] - (when (and coll (not (zero? (count coll)))) + (when (seq coll) (let [a (to-array coll)] (. java.util.Arrays (sort a comp)) (seq a))))) diff --git a/src/clj/clojure/core_proxy.clj b/src/clj/clojure/core_proxy.clj index 8517e238..edbc78fe 100644 --- a/src/clj/clojure/core_proxy.clj +++ b/src/clj/clojure/core_proxy.clj @@ -155,7 +155,7 @@ meths (concat (seq (. c (getDeclaredMethods))) (seq (. c (getMethods))))] - (if meths + (if (seq meths) (let [#^java.lang.reflect.Method meth (first meths) mods (. meth (getModifiers)) mk (method-sig meth)] diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java index 664045fe..b29876f7 100644 --- a/src/jvm/clojure/lang/Compiler.java +++ b/src/jvm/clojure/lang/Compiler.java @@ -40,7 +40,7 @@ public class Compiler implements Opcodes{ static final Symbol DEF = Symbol.create("def"); static final Symbol LOOP = Symbol.create("loop*"); static final Symbol RECUR = Symbol.create("recur"); -static final Symbol IF = Symbol.create("if"); +static final Symbol IF = Symbol.create("if*"); static final Symbol LET = Symbol.create("let*"); static final Symbol DO = Symbol.create("do"); static final Symbol FN = Symbol.create("fn*"); |