clojure icon indicating copy to clipboard operation
clojure copied to clipboard

Clojure sets

Open practicalli-johnny opened this issue 5 years ago • 0 comments

Some aspects of sets are compatible to relational dB SQL apparently

(def users
  #{{:user-id 1 :name "john"   :age 22 :type "personal"}
    {:user-id 2 :name "jake"   :age 28 :type "company"}
    {:user-id 3 :name "amanda" :age 63 :type "personal"}})

(def accounts
  #{{:acc-id 1 :user-id 1 :amount 300.45 :type "saving"}
    {:acc-id 2 :user-id 2 :amount 1200.0 :type "saving"}
    {:acc-id 3 :user-id 1 :amount 850.1 :type "debit"}})

(require '[clojure.set :as s])

;; Clojure equivalent of the SQL:
;; SELECT users.user-id, accounts.acc-id, 
;;        users.type as type, accounts.type as atype
;; FROM users
;; INNER JOIN accounts ON users.user-id = accounts.user-id;

(s/project
  (s/join users (s/rename accounts {:type :atype}))
  [:user-id :acc-id :type :atype])

practicalli-johnny avatar Dec 29 '20 15:12 practicalli-johnny