Skip to content

Instantly share code, notes, and snippets.

@jbowles
Created October 16, 2013 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbowles/7016932 to your computer and use it in GitHub Desktop.
Save jbowles/7016932 to your computer and use it in GitHub Desktop.
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