Skip to content

Storkle/defrecord2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Enhance clojure records to:
 * print in an eval'able form
 * provide clojure function as constructor
 * accept named parameters (maps) in constructor
 * participate in pre/post walk multi-method

David McNeil
August 2010

MODIFICATIONS

* modified defrecord2 macro so i could include protocols
* got rid of print-method and pprint method
* fixed support for print-dup so it outputs #= and also namespace of constructor
* changed position of constructor name

USAGE

(defrecord2 name [a b] Protocol1 ...)
or
(defrecord2 (name constructor) [a b] Protocol1 ...)
and you thus create the name record by

(constructor {:a 2 :b 3})
or, if you use the first one
(new-name {:a 2 :b 3})

TO PRINT-DUP

(binding [*print-dup* true] (print-str record))

This can be saved to a file, using something like this:

;;used from
;;http://groups.google.com/group/clojure/browse_thread/thread/cb5246d07142a3dc?fwc=2&pli=1
(defn frm-save 
 "Save a clojure form to file." 
  [file form] 
  (with-open [w (java.io.FileWriter. 
                 (if (instance? File file) file (File. file)))] 
    (binding [*out* w *print-dup* true] (prn form)))) 
(defn frm-load 
  "Load a clojure form from file." 
  [file] 
  (with-open [r (java.io.PushbackReader. 
     (java.io.FileReader. (if (instance? File file) file (File. 
file))))] 
     (let [rec (read r)] 
       rec)))

and atoms and refs can be included by defining these functions:

(defmethod print-dup clojure.lang.Atom [o w]
  (.write w "#=(clojure.core/atom ") (print-dup @o w) (.write w ")"))
(defmethod print-dup clojure.lang.Ref [o w]
  (.write w "#=(clojure.core/ref ") (print-dup @o w) (.write w ")"))

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Clojure 100.0%