Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various small readability and efficiency tweaks #40

Merged
merged 5 commits into from
May 28, 2024
Merged

Conversation

crisptrutski
Copy link
Collaborator

A few zepto nits that I bit my tongue on in prior reviews.

Comment on lines +81 to +84
(u/strip-nils
{:table (normalize-reference (.getName t) opts)
:schema (normalize-reference (.getSchemaName t) opts)
:instances (when with-instance [t])})))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easier to see the shape of the map this way, IMHO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I've debated internally what to do, but we haven't had utils at the time. 😁

(cond-> (merge a b)
;; collect all instances so we can refer back to maps
(or (-> a :component :instances)
(-> b :component :instances))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to check b's instances, consider this truth table:

a? b? union required? explanation
f f f no instances
f t f we already have b's values
t f t we have b's component, with no instances
t t t we have b's instances only


(def ^:private nil-val? (comp nil? val))

(defn strip-nils
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wanted to get fancy, we could make this an inline function which, given a map literal, saves us building the intermediate map.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given overall performance characteristics I wouldn't worry. :)

(assoc! acc (key-f k) (val-f v)))
(transient {})
m))]
(with-meta ret (meta m))))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the semantics of update-keys and update-vals, to avoid later surprises.

Comment on lines +34 to +38
(let [ret (persistent!
(reduce-kv (fn [acc k v]
(assoc! acc (key-f k) (val-f v)))
(transient {})
m))]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the clojure.core implementations of its sister functions, and saves building a bunch of intermediate vectors.

Comment on lines 110 to 114
(u/strip-nils
{:column (normalize-reference (.getColumnName c) opts)
:alias (let [[k y] (first ctx)]
(when (= k :alias) y))
:instances (when (:with-instance opts) [c])})))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case the difference is even more stark, rather than needing to groc a naming convention, the shape of the map is immediately obvious.

@@ -112,11 +106,12 @@

(defn- make-column [opts ^Column c ctx]
(merge
{:column (normalize-reference (.getColumnName c) opts)}
(maybe-column-alias ctx)
(maybe-column-table opts c)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is a bit nefarious, as it may also return instances which could clobber our own.

Hmm, perhaps it's worth just pulling out what we want instead of merging it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. It's a pity we want multiple keys so we can't get away from the merge.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated again, much happier with destructuring and setting everything explicitly. It's nice not to rely on clobbering.

Comment on lines +81 to +84
(u/strip-nils
{:table (normalize-reference (.getName t) opts)
:schema (normalize-reference (.getSchemaName t) opts)
:instances (when with-instance [t])})))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I've debated internally what to do, but we haven't had utils at the time. 😁


(def ^:private nil-val? (comp nil? val))

(defn strip-nils
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given overall performance characteristics I wouldn't worry. :)

Base automatically changed from handle-quotes to master May 28, 2024 14:08
@crisptrutski crisptrutski merged commit 4583c93 into master May 28, 2024
4 checks passed
@crisptrutski crisptrutski deleted the misc branch May 28, 2024 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants