Skip to content

Commit 8fe19e0

Browse files
rplevydanielcompton
authored andcommitted
Add unsweetened func query term
1 parent 1940f5c commit 8fe19e0

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/rethinkdb/query.cljc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,23 @@
2121
#?(:clj
2222
(:import [rethinkdb.core Connection])))
2323

24+
(defn func
25+
"Unsweetened alternative to fn. The fn macro has the benefit of
26+
providing a syntax that mirrors the native clojure fn, but
27+
oftentimes it's necessary to have the added versatility of a
28+
definition that accepts pure data.
29+
30+
Args is a vector of keywords. Terms is a term map.
31+
e.g. (func [::my-arg] terms)"
32+
[args terms]
33+
(term :FUNC [args terms]))
34+
2435
#?(:clj (defmacro fn [args & [body]]
25-
(let [new-args (into [] (clojure.core/map #(hash-map :temp-var (keyword %)) args))
36+
(let [new-args (into [] (clojure.core/map
37+
#(hash-map :temp-var (keyword %)) args))
2638
new-replacements (zipmap args new-args)
2739
new-terms (walk/postwalk-replace new-replacements body)]
28-
(term :FUNC [new-args new-terms]))))
40+
(func new-args new-terms))))
2941

3042
;;; Import connect
3143

test/rethinkdb/core_test.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,25 @@
303303
x)))})))
304304
conn)))))
305305

306+
(deftest unsweetened-fns
307+
(with-open [conn (r/connect)]
308+
(is (= [{:a {:foo "bar"}
309+
:b [1 2]}]
310+
(r/run (-> [{:foo "bar"}]
311+
(r/map (r/func [::x]
312+
{:a ::x
313+
:b (-> [1 2]
314+
(r/map (r/func [::x] ::x)))})))
315+
conn)))
316+
(is (= [{:a {:foo "bar"}
317+
:b [{:foo "bar"} {:foo "bar"}]}]
318+
(r/run (-> [{:foo "bar"}]
319+
(r/map (r/func [::x]
320+
{:a ::x
321+
:b (-> [1 2]
322+
(r/map (r/func [::y] ::x)))})))
323+
conn)))))
324+
306325
(deftest filter-with-default
307326
(with-open [conn (r/connect)]
308327
(let [twin-peaks [{:name "Cole", :job "Regional Bureau Chief"}

0 commit comments

Comments
 (0)