Skip to content

Commit a646036

Browse files
bfontaineJulianBirch
authored andcommitted
Remove defn-curried
Fixes #255.
1 parent 39396ea commit a646036

File tree

6 files changed

+67
-103
lines changed

6 files changed

+67
-103
lines changed

src/ajax/formats.cljc

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
url, json and transit are found in their own files."
44
(:require [ajax.interceptors :as i]
55
[ajax.util :as u]
6-
[ajax.protocols :as pr]
7-
#? (:clj [ajax.macros :as m]))
6+
[ajax.protocols :as pr])
87
#? (:clj (:import [java.io InputStream]
9-
[java.util Scanner])
10-
:cljs (:require-macros [ajax.macros :as m])))
8+
[java.util Scanner])))
119

1210
(defn raw-response-format
1311
"This will literally return whatever the underlying implementation
@@ -47,7 +45,7 @@
4745

4846
;;; Detect Response Format
4947

50-
(m/defn-curried get-format [request format-entry]
48+
(defn get-format [request format-entry]
5149
"Converts one of a number of types to a response format.
5250
Note that it processes `[text format]` the same as `format`,
5351
which makes it easier to work with detection vectors such as
@@ -65,41 +63,44 @@
6563
;;; Must be a format generating function
6664
:else (format-entry request)))
6765

68-
(m/defn-curried get-accept-entries [request format-entry]
66+
(defn get-accept-entries [request format-entry]
6967
(let [fe (if (vector? format-entry)
7068
(first format-entry)
7169
(:content-type (get-format request format-entry)))]
7270
(cond (nil? fe) ["*/*"]
7371
(string? fe) [fe]
7472
:else fe)))
7573

76-
(m/defn-curried content-type-matches
74+
(defn content-type-matches
7775
[^String content-type ^String accept]
7876
(or (= accept "*/*")
7977
(>= (.indexOf content-type accept) 0)))
8078

81-
(m/defn-curried detect-content-type
79+
(defn detect-content-type
8280
[content-type request format-entry]
8381
(let [accept (get-accept-entries request format-entry)]
84-
(some (content-type-matches content-type) accept)))
82+
(some #(content-type-matches content-type %) accept)))
8583

8684
(defn get-default-format
8785
[response {:keys [response-format] :as request}]
88-
(let [f (detect-content-type (u/get-content-type response) request)]
89-
(->> response-format
90-
(filter f)
91-
first
92-
(get-format request))))
86+
(let [content-type (u/get-content-type response)]
87+
(letfn [(accepted-format?
88+
[format-entry]
89+
(detect-content-type content-type request format-entry))]
90+
(->> response-format
91+
(filter accepted-format?)
92+
first
93+
(get-format request)))))
9394

94-
(m/defn-curried detect-response-format-read
95-
[request response]
96-
(let [format (get-default-format response request)]
97-
((:read format) response)))
95+
(defn detect-response-format-read
96+
[request]
97+
(fn detect-response-format [response]
98+
(let [format (get-default-format response request)]
99+
((:read format) response))))
98100

99101
(defn accept-header [{:keys [response-format] :as request}]
100-
(if (vector? response-format)
101-
(mapcat (get-accept-entries request) response-format)
102-
(get-accept-entries request response-format)))
102+
(let [formats (if (vector? response-format) response-format [response-format])]
103+
(mapcat #(get-accept-entries request %) formats)))
103104

104105
(defn detect-response-format [opts]
105106
"NB This version of the response format doesn't have a zero

src/ajax/interceptors.cljc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
headers))))
171171
(-process-response [_ xhrio] xhrio))
172172

173-
(m/defn-curried ^:internal uri-with-params [{:keys [vec-strategy params method url-params]} uri]
173+
(defn ^:internal uri-with-params [{:keys [vec-strategy params method url-params]} uri]
174174
"Internal function. Takes a uri and appends the interpretation of the query string to it
175175
matching the behaviour of `url-request-format`."
176176
(if-let [final-url-params (if (and (= method "GET") (nil? url-params))
@@ -189,9 +189,11 @@
189189
(defrecord ProcessUrlParameters []
190190
Interceptor
191191
(-process-request [_ {:keys [method] :as request}]
192-
(let [if-get-reduce (if (= method "GET") reduced identity)]
193-
(if-get-reduce (update request :uri
194-
(uri-with-params request)))))
192+
(cond->
193+
(update request :uri
194+
(partial uri-with-params request))
195+
(= method "GET")
196+
reduced))
195197
(-process-response [_ response] response))
196198

197199
;;; DirectSubmission is one of the default interceptors.

src/ajax/macros.clj

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,3 @@
2222
(if (keyword? f#)
2323
(apply hash-map ~opts)
2424
f#))))))
25-
26-
(defn- curry
27-
[[params1 params2] body]
28-
(cons (vec params1)
29-
(if (empty? params2)
30-
body
31-
(list (apply list 'fn (vec params2) body)))))
32-
33-
(defn- do-curried [symbol to-fn params]
34-
(let [result (split-with (complement vector?) params)
35-
[[name doc meta] [args & body]] result
36-
[doc meta] (if (string? doc) [doc meta] [nil doc])
37-
body (if meta (cons meta body) body)
38-
arity-for-n #(-> % inc (split-at args) (to-fn body))
39-
arities (->>
40-
(range 0 (count args))
41-
(map arity-for-n)
42-
reverse)
43-
before (keep identity [symbol name doc])]
44-
(concat before arities)))
45-
46-
(defmacro defn-curried
47-
"Builds a multiple arity function similar that returns closures
48-
for the missing parameters, similar to ML's behaviour."
49-
[& params]
50-
(do-curried 'defn curry params))
51-
52-
(defmacro defn-curried-
53-
"Builds a multiple arity function similar that returns closures
54-
for the missing parameters, similar to ML's behaviour."
55-
[& params]
56-
(do-curried 'defn- curry params))
57-
58-
(defmacro fn-curried
59-
"Builds a multiple arity function similar that returns closures
60-
for the missing parameters, similar to ML's behaviour."
61-
[& params]
62-
(do-curried 'fn curry params))

src/ajax/simple.cljc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
(defn process-response [response interceptor]
1919
(pr/-process-response interceptor response))
2020

21-
(m/defn-curried js-handler [handler interceptors response]
22-
(let [processed (reduce process-response response interceptors)]
23-
;;; This requires a bit of explanation: if we return a closeable,
24-
;;; it should be wrapping the original response, so we _don't_
25-
;;; close the original response stream
26-
;;; If you're writing a weird interceptor that doesn't do this,
27-
;;; remember to close the original stream yourself
28-
#? (:clj (if (and response
29-
(instance? Closeable (second processed)))
30-
(.close ^Closeable (pr/-body response))))
31-
(handler processed)))
21+
(defn make-js-handler [handler interceptors]
22+
(fn js-handler [response]
23+
(let [processed (reduce process-response response interceptors)]
24+
;;; This requires a bit of explanation: if we return a closeable,
25+
;;; it should be wrapping the original response, so we _don't_
26+
;;; close the original response stream
27+
;;; If you're writing a weird interceptor that doesn't do this,
28+
;;; remember to close the original stream yourself
29+
#?(:clj (if (and response
30+
(instance? Closeable (second processed)))
31+
(.close ^Closeable (pr/-body response))))
32+
(handler processed))))
3233

3334
(defn base-handler [interceptors {:keys [handler]}]
3435
(if handler
35-
(js-handler handler interceptors)
36+
(make-js-handler handler interceptors)
3637
(u/throw-error "No ajax handler provided.")))
3738

3839
(def default-interceptors (atom []))

src/ajax/transit.cljc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
(:require [cognitect.transit :as t]
33
[ajax.interceptors :as i]
44
[ajax.protocols :as pr]
5-
[ajax.util :as u]
6-
#? (:clj [ajax.macros :as m])))
7-
; Surprisingly, only the clj version needs m/defn-curried
5+
[ajax.util :as u]))
86

97
(defn transit-type [{:keys [type]}]
108
(or type #? (:cljs :json :clj :msgpack)))
@@ -15,10 +13,11 @@
1513
(t/writer type opts))]
1614
(fn transit-write-params [params]
1715
(t/write writer params))))
18-
:clj (m/defn-curried transit-write-fn
19-
[type opts stream params]
20-
(let [writer (t/writer stream type opts)]
21-
(t/write writer params))))
16+
:clj (defn transit-write-fn
17+
[type opts]
18+
(fn transit-write-params [stream params]
19+
(let [writer (t/writer stream type opts)]
20+
(t/write writer params)))))
2221

2322
(defn transit-request-format
2423
"Returns a Transit request format.
@@ -41,13 +40,14 @@
4140
(t/reader :json opts))]
4241
(fn transit-read-response [response]
4342
(t/read reader (pr/-body response)))))
44-
:clj (m/defn-curried transit-read-fn [request response]
45-
(let [content-type (u/get-content-type response)
46-
type (if (.contains content-type "msgpack")
47-
:msgpack :json)
48-
stream (pr/-body response)
49-
reader (t/reader stream type request)]
50-
(t/read reader))))
43+
:clj (defn transit-read-fn [request]
44+
(fn transit-read-response [response]
45+
(let [content-type (u/get-content-type response)
46+
type (if (.contains content-type "msgpack")
47+
:msgpack :json)
48+
stream (pr/-body response)
49+
reader (t/reader stream type request)]
50+
(t/read reader)))))
5151

5252
(defn transit-response-format
5353
"Returns a Transit request format.

src/ajax/url.cljc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@
4646
of cljs-ajax. Now we have ~50 SLOCs achieving much the same result.
4747
"
4848

49-
#?@ (:clj ((:require
50-
[ajax.macros :as m]
51-
[ajax.util :as u]
52-
[clojure.string :as str]))
53-
:cljs ((:require
54-
[clojure.string :as str]
55-
[ajax.util :as u])
56-
(:require-macros [ajax.macros :as m]))))
49+
#? (:clj (:require
50+
[ajax.util :as u]
51+
[clojure.string :as str])
52+
:cljs (:require
53+
[clojure.string :as str]
54+
[ajax.util :as u])))
5755

5856

5957
(defn- key-encode [key]
@@ -66,18 +64,18 @@
6664
(defn- key-value-pair-to-str [[k v]]
6765
(str (key-encode k) "=" (value-encode v)))
6866

69-
(m/defn-curried- vec-key-transform-fn [vec-key-encode k v]
67+
(defn- vec-key-transform-fn [vec-key-encode k v]
7068
[(vec-key-encode k) v])
7169

7270
(defn- to-vec-key-transform [vec-strategy]
7371
(let [vec-key-encode (case (or vec-strategy :java)
7472
:java (fn [k] nil) ; no subscript
7573
:rails (fn [k] "") ; [] subscript
7674
:indexed identity)] ; [1] subscript
77-
(vec-key-transform-fn vec-key-encode)))
75+
(partial vec-key-transform-fn vec-key-encode)))
7876

7977

80-
(m/defn-curried- param-to-key-value-pairs [vec-key-transform prefix [key value]]
78+
(defn- param-to-key-value-pairs [vec-key-transform prefix [key value]]
8179
"Takes a parameter and turns it into a sequence of key-value pairs suitable
8280
for passing to `key-value-pair-to-str`. Since we can have nested maps and
8381
vectors, we need a vec-key-transform function and the current query key
@@ -89,7 +87,7 @@
8987
(str prefix "[" k1 "]")
9088
prefix)
9189
k1)
92-
recurse (param-to-key-value-pairs vec-key-transform new-key)]
90+
recurse (partial param-to-key-value-pairs vec-key-transform new-key)]
9391
(cond
9492
(string? value) ; string is sequential so we have to handle it separately
9593
[[new-key value]] ; ("a" 1) should be ["a" 1]
@@ -107,7 +105,7 @@
107105

108106
:else [[new-key value]])))
109107

110-
(m/defn-curried params-to-str [vec-strategy params]
108+
(defn params-to-str [vec-strategy params]
111109
"vec-strategy is one of :rails (a[]=3&a[]=4)
112110
:java (a=3&a=4) (this is the correct behaviour and the default)
113111
:indexed (a[3]=1&a[4]=1)
@@ -121,5 +119,5 @@
121119
"The request format for simple POST and GET."
122120
([] (url-request-format {}))
123121
([{:keys [vec-strategy]}]
124-
{:write (u/to-utf8-writer (params-to-str vec-strategy))
122+
{:write (u/to-utf8-writer (partial params-to-str vec-strategy))
125123
:content-type "application/x-www-form-urlencoded; charset=utf-8"}))

0 commit comments

Comments
 (0)