Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
quick example of defrecord
;; record Record
(defrecord User [fname lname address])
;; ActiveRecord generally (Object.Model), instead namespace.Record
(defrecord Address [street city state zip])
(defrecord Foo [a b c])
(class Foo) ;java.lang.Class
;; Create an instance of the User and Address records
(def stu (User. "FirstName" "LastName"
(Address. "300 N Street"
"Somewhere"
"Someplace"
890876)))
(:lname stu)
(:fname stu)
(:address stu)
(-> stu :address :city)
(-> stu :address :zip)
(assoc stu :fname "Joshua")
; "Joshua"
(:fname stu)
; "FirstName"
(update-in stu [:address :zip] inc)
; 89076
(-> stu :address :zip)
; 89077
;; map from one record to another
(def foo-instance (Foo. 10 20 30))
(println foo-instance)
(def foo-mapped-user (map->User (merge foo-instance {:fname nil})))
(println foo-mapped-user)
(def foo-mapped-user-two (map->User (merge foo-instance {:stu (:lname stu)})))
(println foo-mapped-user-two)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.