From 5014d97a6ec5a0e6e1b526c101bf9fb7b8385d75 Mon Sep 17 00:00:00 2001 From: Jonathan Smith Date: Wed, 26 Aug 2009 19:14:18 -0400 Subject: [PATCH] n-ary bit ops, inline nary math and bit ops --- src/clj/clojure/core.clj | 116 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 116 insertions(+), 0 deletions(-) diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index 992989e..23219a2 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -4253,6 +4253,122 @@ (load "core_print") (load "genclass") +(defn- rule-set [rule] + (proxy [clojure.lang.IPersistentSet] [] + (disjoin [key] + (rule-set + (fn [x] + (when (not= x key) + (rule x))))) + (contains [key] + (if (rule key) + true + false)) + (get [key] + (when (rule key) + key)) + (count [] + (throw (Exception.))) + (cons [key] + (rule-set + (fn [x] + (if (= x key) + true + (rule x))))) + (empty [] + (throw (Exception.))) + (equiv [a] + nil) + (seq [] (list {:ruleset rule})))) + + (let [>1-arities (rule-set (fn [x] (> x 1))) + >0-arities (rule-set (fn [x] (> x 1))) + all-arities (rule-set (fn [x] (>= x 0))) + even-arities (rule-set (fn [x] (and (== (mod x 2) 0) (not (< x 1)))))] + + (let [math-inlining (fn [op] + (fn ([x y] `(. clojure.lang.Numbers (~op ~x ~y))) + ([x y & more] + (reduce + (fn [a b] `(. clojure.lang.Numbers (~op ~a ~b))) + `(. clojure.lang.Numbers (~op ~x ~y)) more))))] +;;bit stuff + (defn bit-and + "Bitwise and" + {:inline (math-inlining 'and) + :inline-arities >1-arities} + ([x y] (. clojure.lang.Numbers and x y)) + ([x y & more] (reduce bit-and (bit-and x y) more))) + + (defn bit-or + "Bitwise or" + {:inline (math-inlining 'or) + :inline-arities >1-arities} + ([x y] (. clojure.lang.Numbers or x y)) + ([x y & more] (reduce bit-or (bit-or x y) more))) + + (defn bit-xor + "Bitwise exclusive or" + {:inline (math-inlining 'xor) + :inline-arities >1-arities} + ([x y] (. clojure.lang.Numbers xor x y)) + ([x y & more] (reduce bit-xor (bit-xor x y) more))) + + (defn bit-and-not + "Bitwise and with complement" + {:inline (math-inlining 'andNot) + :inline-arities >1-arities} + ([x y] (. clojure.lang.Numbers andNot x y)) + ([x y & more] (reduce bit-and-not (bit-and-not x y) more))) + + ;;math stuff + (defn + + "Returns the sum of nums. (+) returns 0." + {:inline (math-inlining 'add) + :inline-arities >1-arities} + ([] 0) + ([x] (cast Number x)) + ([x y] (. clojure.lang.Numbers (add x y))) + ([x y & more] + (reduce + (+ x y) more))) + + (defn * + "Returns the product of nums. (*) returns 1." + {:inline (math-inlining 'multiply) + :inline-arities >1-arities} + ([] 1) + ([x] (cast Number x)) + ([x y] (. clojure.lang.Numbers (multiply x y))) + ([x y & more] + (reduce * (* x y) more))) + + (defn / + "If no denominators are supplied, returns 1/numerator, + else returns numerator divided by all of the denominators." + {:inline (fn + ([x y] `(. clojure.lang.Numbers (divide ~x ~y))) + ([x y & more] `(. clojure.lang.Numbers (divide ~x (* ~y ~@more))))) + :inline-arities >1-arities} + ([x] (/ 1 x)) + ([x y] (. clojure.lang.Numbers (divide x y))) + ([x y & more] + (/ x (reduce * y more)))) + + (defn - + "If no ys are supplied, returns the negation of x, else subtracts + the ys from x and returns the result." + {:inline (fn ([x] `(. clojure.lang.Numbers (minus ~x))) + ([x y] `(. clojure.lang.Numbers (minus ~x ~y))) + ([x y & more] + (reduce (fn [a b] + `(. clojure.lang.Numbers (minus ~a ~b))) + `(. clojure.lang.Numbers (minus ~x ~y)) more))) + :inline-arities >0-arities} + ([x] (. clojure.lang.Numbers (minus x))) + ([x y] (. clojure.lang.Numbers (minus x y))) + ([x y & more] + (reduce - (- x y) more))))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; futures (needs proxy);;;;;;;;;;;;;;;;;; (defn future-call "Takes a function of no args and yields a future object that will -- 1.6.0.4