Skip to content

Commit 3422915

Browse files
committed
Improving the implementation of distinct-except (and swapping the argument order).
1 parent 35bab7d commit 3422915

3 files changed

Lines changed: 14 additions & 19 deletions

File tree

src/yesql/types.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
query-args (mapv replace-question-mark-with-gensym args)]
2727
{:query-args query-args
2828
:function-args (distinct query-args)
29-
:display-args (distinct-except args #{'?})}))
29+
:display-args (distinct-except #{'?} args)}))
3030

3131
(defn- fn-symbol
3232
"Attach metadata (docstring/argument lists) to the given symbol."

src/yesql/util.clj

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@
1010

1111
(ann distinct-except
1212
(All [x]
13-
[(Option (Seq x)) (Option (Set x)) -> (Option (Seq x))]))
13+
[[x -> Boolean] (Option (Seq x)) -> (Option (Seq x))]))
1414
(defn distinct-except
15-
"Same as distinct, but keeps duplicates from the exceptions set."
16-
[coll exceptions]
17-
{:pre [(coll? coll)
18-
(set? exceptions)]}
19-
(let [step (fn step [xs seen]
20-
(lazy-seq
21-
((fn [[f :as xs] seen]
22-
(when-let [s (seq xs)]
23-
(if (and (contains? seen f)
24-
(not (exceptions f)))
25-
(recur (rest s) seen)
26-
(cons f (step (rest s) (conj seen f))))))
27-
xs seen)))]
28-
(step coll #{})))
15+
"Same as distinct, but keeps duplicates if they pass exception?"
16+
[exception? [head & tail :as coll]]
17+
(lazy-seq
18+
(when head
19+
(cons head
20+
(distinct-except exception?
21+
(if (exception? head)
22+
tail
23+
(remove (partial = head) tail)))))))
2924

3025
(ann whitespace?
3126
(IFn [nil -> false]

test/yesql/util_test.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
;;; Test distinct-except
77
(let [coll '[a b c a b]]
88
(expect '[a b c a]
9-
(distinct-except coll #{'a}))
9+
(distinct-except #{'a} coll))
1010
(expect '[a b c b]
11-
(distinct-except coll #{'b}))
11+
(distinct-except #{'b} coll))
1212
(expect '[a b c]
13-
(distinct-except coll #{'c})))
13+
(distinct-except #{'c} coll)))
1414

1515
;;; Test underscores-to-dashes
1616
(do-template [input output]

0 commit comments

Comments
 (0)