Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I improve this Clojure function?

Tags:

clojure

I just wrote my first Clojure function based on my very limited knowledge of the language. I would love some feedback in regards to performance and use of types. For example, I'm not sure if I should be using lists or vectors.

(defn actor-ids-for-subject-id [subject-id]
  (sql/with-connection (System/getenv "DATABASE_URL")
    (sql/with-query-results results
      ["SELECT actor_id FROM entries WHERE subject_id = ?" subject-id]
      (let [res (into [] results)]
        (map (fn [row] (get row :actor_id)) res)))))

It passes the following test (given proper seed data):

(deftest test-actor-ids-for-subject-id
  (is (= ["123" "321"] (actor-ids-for-subject-id "123"))))

If it makes a difference (and I imagine it does) my usage characteristics of the returned data will almost exclusively involve generating the union and intersection of another set returned by the same function.

like image 700
bloudermilk Avatar asked Mar 26 '26 18:03

bloudermilk


1 Answers

it's slightly more concise to use 'vec' instead of 'into' when the initial vector is empty. it may express the intent more clearly, though that's more a matter of preference.

(vec (map :actor_id results))
like image 193
Arthur Ulfeldt Avatar answered Apr 02 '26 08:04

Arthur Ulfeldt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!