Browse files

* src/clj_nehe/utils.clj: very simplistic check to see if something h…

…as already been casted. prim is an all or nothing affair. either use it to cast your math or don't.
  • Loading branch information...
swannodette committed Apr 13, 2010
1 parent be180ea commit 9248c6193fcecd1fb181386a7d89877952a0afa9
Showing with 7 additions and 7 deletions.
  1. +7 −7 src/clj_nehe/utils.clj
@@ -2,20 +2,20 @@
(:use [clojure.walk :only [postwalk]]
[clojure.contrib.macro-utils :only [mexpand-all]]))
+(defn casted? [expr]
+ (when-let [[_ x] expr]
+ (when (and (coll? x) (#{'float 'int 'double 'long 'short 'byte} (first x)))
+ true)))
(defn to-prim-op [type]
(fn [expr]
(if-let [op (and (coll? expr)
+ (not (casted? expr))
(#{'+ '- '* '/ '< '> '=} (first expr)))]
(concat `(prim ~type ~op) (rest expr))
(defmacro prim
- "If given an type and expression it will macroexpand the expression fully
- and then convert all primitive operations into binary operations and
- type-hint all parameters.
- If given a type, fn, and expression(s) it does not macroexpand all
- subexpressions. Basically you have to be somewhat careful how you use
- -> in conjuction with prim."
([type a]
`(~type ~(postwalk (to-prim-op type) (mexpand-all a))))
([type fn a]
@@ -24,7 +24,7 @@
`(~fn (~type ~((to-prim-op type) a)) (~type ~((to-prim-op type) b))))
([type fn a b & rest]
`(~fn (~type ~((to-prim-op type) a))
- (prim ~type ~fn ~b ~@rest))))
+ (prim ~type ~fn ~b ~@rest))))

0 comments on commit 9248c61

Please sign in to comment.