Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed curried functions

  • Loading branch information...
commit 39806b3c60bfddacb846bb6fbe4f3390c4d6c357 1 parent cf2051d
Dmitri Naumov authored
2  src/contracts/core.clj
@@ -80,4 +80,6 @@
80 80 (for [clause clauses]
81 81 `(provide-contract ~@clause))))
82 82
  83 +
83 84 (load "preds")
  85 +(load "curried") ; this line should be commented out during development
21 src/contracts/curried.clj
... ... @@ -1,13 +1,16 @@
  1 +(in-ns 'contracts.core)
  2 +
1 3 (defmacro ^:private defcurried
2   - ([name]
3   - (let [clj-var (ns-resolve 'clojure.core name)]
4   - `(-> (defn ~name
5   - ([a#] (partial ~clj-var a#))
6   - ([a# b#] (~clj-var a# b#)))
7   - (alter-meta! merge (meta ~clj-var)))))
8   - ([name & names]
9   - `(do (defcurried ~name)
10   - (defcurried ~@names))))
  4 + [& names]
  5 + (->> (for [name names
  6 + :let [clj-var (ns-resolve 'clojure.core name)
  7 + doc (str "Curried version of clojure.core/" name)
  8 + args (->> clj-var meta :arglists (take 2))]]
  9 + `(do (defn ~name
  10 + ([a#] (partial ~clj-var a#))
  11 + ([a# b#] (~clj-var a# b#)))
  12 + (alter-meta! (var ~name) assoc :doc ~doc :arglists '~args)))
  13 + (cons `do)))
11 14
12 15 (defcurried
13 16 = == not= < > <= >=
4 test/contracts/test/core.clj
... ... @@ -1,5 +1,5 @@
1 1 (ns contracts.test.core
2   - (:use [contracts.core :as c :exclude [=>]] :reload)
  2 + (:require [contracts.core :as c] :reload)
3 3 (:use [midje.sweet]))
4 4
5 5 (fact "Simple contracts with explicit arguments"
@@ -101,7 +101,7 @@
101 101 (defn constrained-inc [x] (inc x))
102 102 (defn constrained-dec [x] (dec x))
103 103
104   -(provide-contracts
  104 +(c/provide-contracts
105 105 (constrained-inc (c/=> number? number?))
106 106 (constrained-dec [number? => number?]))
107 107
17 test/contracts/test/tutorial.clj
@@ -187,7 +187,7 @@
187 187
188 188 (provide-contract sum
189 189 (c/=> ([(c/coll-of number?)]
190   - [fn? (c/coll-of number?)])
  190 + [fn? (c/coll-of number?)])
191 191 number?))
192 192
193 193 ;; As you can see, we've wrapped preconditions for different arities
@@ -239,7 +239,7 @@
239 239
240 240 (provide-contract sum
241 241 (c/=> ([(c/coll-of number?)]
242   - [(c/=> number? number?) (c/coll-of number?)])
  242 + [(c/=> number? number?) (c/coll-of number?)])
243 243 number?))
244 244
245 245 ;; We've replaced `fn?` predicate with another `c/=>` call. That's
@@ -358,7 +358,12 @@
358 358 (c/=> [x y]
359 359 {x (c/and number? (c/not neg?))
360 360 y (c/and number? (c/not neg?))}
361   - (partial <= (Math/sqrt (* x y)))))
  361 + (c/<= (Math/sqrt (* x y)))))
  362 +
  363 +;; Pay attention to the `c/<=` call; for your convenience,
  364 +;; clojure-contracts includes curried versions of the most commonly
  365 +;; used predicates from clojure.core, so it's possible to simply write
  366 +;; `(c/<= ...)` instead of `(partial <= ...)`.
362 367
363 368 ;; Of course, this contract will never be violated as long as valid
364 369 ;; input is given to the function (which is ensured by preconditions)
@@ -422,11 +427,11 @@
422 427
423 428 (provide-contracts
424 429 (forward (c/=> [this]
425   - {this (partial satisfies? Movement)}
  430 + {this (c/satisfies? Movement)}
426 431 (fn [result]
427 432 (ahead? result this))))
428 433 (backward (c/=> [this]
429   - {this (partial satisfies? Movement)}
  434 + {this (c/satisfies? Movement)}
430 435 (fn [result]
431 436 (ahead? this result)))))
432 437
@@ -488,7 +493,7 @@
488 493 ;; provide the appropriate contract. Let's add some implementations
489 494 ;; and check how they work:
490 495
491   -(comment ;; ** TODO **
  496 +(comment
492 497 (defmethod mean :arithmetic [x y]
493 498 (/ (+ x y) 2))
494 499

0 comments on commit 39806b3

Please sign in to comment.
Something went wrong with that request. Please try again.