Created
October 16, 2013 23:44
-
-
Save jbowles/7016932 to your computer and use it in GitHub Desktop.
quick example of defrecord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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