Added defrules macro

This commit is contained in:
Jonas Enlund 2012-03-18 15:23:06 +02:00
parent ce13cefb50
commit 92552efa59
5 changed files with 73 additions and 67 deletions

View file

@ -25,12 +25,12 @@
;; A map of the individual rule sets, keyed by rule group
#_(def rule-map {:control-structures control/rules
(def rule-map {:control-structures control/rules
:arithmetic arith/rules
:collections coll/rules
:misc misc/rules})
;; TODO: Consider a refactor for this into a function
;; `(defn rules-for-ns [& namespaces])`
(def all-rules misc/rules);(apply merge (vals rule-map)))
(def all-rules (apply concat (vals rule-map)))

View file

@ -1,22 +1,23 @@
(ns jonase.kibit.rules.arithmetic)
(ns jonase.kibit.rules.arithmetic
(:use [jonase.kibit.rules.util :only [defrules]]))
(def rules
'{(+ ?x 1) (inc ?x)
(+ 1 ?x) (inc ?x)
(- ?x 1) (dec ?x)
(defrules rules
[(+ ?x 1) (inc ?x)]
[(+ 1 ?x) (inc ?x)]
[(- ?x 1) (dec ?x)]
(= 0 ?x) (zero? ?x)
(= ?x 0) (zero? ?x)
(== 0 ?x) (zero? ?x)
(== ?x 0) (zero? ?x)
[(= 0 ?x) (zero? ?x)]
[(= ?x 0) (zero? ?x)]
[(== 0 ?x) (zero? ?x)]
[(== ?x 0) (zero? ?x)]
(< 0 ?x) (pos? ?x)
(> ?x 0) (pos? ?x)
(<= 1 ?x) (pos? ?x)
[(< 0 ?x) (pos? ?x)]
[(> ?x 0) (pos? ?x)]
[(<= 1 ?x) (pos? ?x)]
(< ?x 0) (neg? ?x)
[(< ?x 0) (neg? ?x)]
(= ?x ?x) true
(== ?x ?x) true
(zero? 0) true})
[(= ?x ?x) true]
[(== ?x ?x) true]
[(zero? 0) true])

View file

@ -1,10 +1,11 @@
(ns jonase.kibit.rules.collections)
(ns jonase.kibit.rules.collections
(:use [jonase.kibit.rules.util :only [defrules]]))
(def rules
'{;;vector
(conj [] . ?x) (vector . ?x)
(into [] ?coll) (vec ?coll)
(defrules rules
;;vector
[(conj [] . ?x) (vector . ?x)]
[(into [] ?coll) (vec ?coll)]
;; set
(into #{} ?coll) (set ?coll)})
;; set
[(into #{} ?coll) (set ?coll)])

View file

@ -1,17 +1,18 @@
(ns jonase.kibit.rules.control-structures)
(ns jonase.kibit.rules.control-structures
(:use [jonase.kibit.rules.util :only [defrules]]))
(def rules
'{(if ?x ?y nil) (when ?x ?y)
(if ?x nil ?y) (when-not ?x ?y)
(if ?x (do . ?y)) (when ?x ?y)
(if (not ?x) ?y ?z) (if-not ?x ?y ?z)
(when (not ?x) . ?y) (when-not ?x . ?y)
(if true ?x ?y) ?x
(when true . ?x) (do . ?x)
(do ?x) ?x
(when-not true ?x) "removing this dead code"
(when false ?x) "removing this dead code"
(if-let ?binding ?expr nil) (when-let ?binding ?expr)})
(defrules rules
[(if ?x ?y nil) (when ?x ?y)]
[(if ?x nil ?y) (when-not ?x ?y)]
[(if ?x (do . ?y)) (when ?x ?y)]
[(if (not ?x) ?y ?z) (if-not ?x ?y ?z)]
[(when (not ?x) . ?y) (when-not ?x . ?y)]
[(if true ?x ?y) ?x]
[(when true . ?x) (do . ?x)]
[(do ?x) ?x]
[(when-not true ?x) "removing this dead code"]
[(when false ?x) "removing this dead code"]
[(if-let ?binding ?expr nil) (when-let ?binding ?expr)])
(comment
(when (not (pred? x y)) (f x y))

View file

@ -1,5 +1,7 @@
(ns jonase.kibit.rules.misc
(:use [clojure.core.logic :only [defne project pred]]))
(:use
[jonase.kibit.rules.util :only [defrules]]
[clojure.core.logic :only [defne project pred]]))
(defn not-method? [sym]
@ -11,35 +13,36 @@
(pred fun symbol?)
(pred fun not-method?))))
(def rules
[;; clojure.string
['(apply str (interpose ?x ?y)) [] '(clojure.string/join ?x ?y)]
['(apply str (reverse ?x)) [] '(clojure.string/reverse ?x)]
(defrules rules
;; clojure.string
[(apply str (interpose ?x ?y)) (clojure.string/join ?x ?y)]
[(apply str (reverse ?x)) (clojure.string/reverse ?x)]
;; mapcat
['(apply concat (apply map ?x ?y)) [] '(mapcat ?x ?y)]
['(apply concat (map ?x . ?y)) [] '(mapcat ?x . ?y)]
;; filter
['(filter (complement ?pred) ?coll) [] '(remove ?pred ?coll)]
['(filter #(not (?pred ?x)) ?coll) [] '(remove ?pred ?coll)]
;; Unneeded anonymous functions -- see bug #16
['(fn ?args (?fun . ?args)) [fn-call?] '?fun]
['(fn* ?args (?fun . ?args)) [fn-call?] '?fun]
;; do
['(do ?x) [] '?x]
;; Java stuff
['(.toString ?x) [] '(str ?x)]
;; Threading
['(-> ?x ?y) [] '(?y ?x)]
['(->> ?x ?y) [] '(?y ?x)]
;; Other
['(not (= . ?args)) [] '(not= . ?args)]])
;; mapcat
[(apply concat (apply map ?x ?y)) (mapcat ?x ?y)]
[(apply concat (map ?x . ?y)) (mapcat ?x . ?y)]
;; filter
[(filter (complement ?pred) ?coll) (remove ?pred ?coll)]
[(filter #(not (?pred ?x)) ?coll) (remove ?pred ?coll)]
;; Unneeded anonymous functions -- see bug #16
[(fn ?args (?fun . ?args)) [fn-call?] ?fun]
[(fn* ?args (?fun . ?args)) [fn-call?] ?fun]
;; do
[(do ?x) ?x]
;; Java stuff
[(.toString ?x) (str ?x)]
;; Threading
[(-> ?x ?y) (?y ?x)]
[(->> ?x ?y) (?y ?x)]
;; Other
[(not (= . ?args)) (not= . ?args)])
(comment
(apply concat (apply map f (apply str (interpose \, "Hello"))))