Permalink
Browse files

Working on curried functors

  • Loading branch information...
1 parent d39f3d7 commit 0b8c4e5e50e9bf9aa031d4bb9687ed01ba4b96b1 @fogus fogus committed Sep 14, 2012
@@ -1,5 +1,6 @@
(ns clojure.core.contracts
"The public contracts programming functions and macros for clojure.core.contracts."
+ (:refer-clojure :exclude [== = not=])
(:use [clojure.core.contracts.impl.transformers :only (build-contract-fn-body)])
(:require [clojure.core.contracts.impl.utils :as tools]))
@@ -45,3 +46,7 @@
(list `alter-var-root (list `var n#)
(list `fn '[f c] (list `with-constraints 'f 'c)) c#))
nil)))
+
+(tools/defcurry-all clojure.core
+ == = not=)
+
@@ -25,3 +25,49 @@
"[pre-conditions => post-conditions] or "
"{:pre [pre-conditions]}"
"for record type " name)))
+
+;; Currying
+
+(defn do-curried
+ [name doc meta args body]
+ (let [cargs (vec (butlast args))]
+ `(defn ~name ~doc ~meta
+ (~cargs (fn [x#] (~name ~@cargs x#)))
+ (~args ~@body))))
+
+(defmacro defcurried
+ "Builds another arity of the fn that returns a fn awaiting the last
+ param"
+ [name doc meta args & body]
+ (do-curried name doc meta args body))
+
+(defmacro defcurry-all
+ "Builds a pass-through curried fn for each name."
+ [namespace & names]
+ (->> (for [n names]
+ (let [v (ns-resolve namespace n)]
+ `(defcurried ~n
+ ~(str "Curried version of " v)
+ {:clojure.core.contracts/original ~v}
+ [l# r#]
+ (~v l# r#))))
+ (cons `do)))
+
+(comment
+ (macroexpand
+ '(defcurry-all clojure.core
+ == =))
+
+
+
+ ((== 1) 1)
+
+ (defcurried ===
+ "test"
+ {:added "1.5"}
+ [l r]
+ (== l r))
+
+ ((=== 1) 2)
+
+)
@@ -1,6 +1,6 @@
(ns clojure.core.contracts-tests
- (:use [clojure.core.contracts :only (contract with-constraints provide)]
- [clojure.test :only [deftest is]]
+ (:require [clojure.core.contracts :as c :refer (contract)])
+ (:use [clojure.test :only [deftest is]]
clojure.core.contracts.impl.transformers))
(defn defer [desc]
@@ -18,6 +18,12 @@
(is (= 25 (sqr -5)))
(is (thrown? AssertionError (sqr 0))))
+(comment
+
+ (sqr 0)
+
+)
+
;; # Contract name use

0 comments on commit 0b8c4e5

Please sign in to comment.