Extensible Data Notation

Notice that, so far, we've just written out lists of keywords. The output file shows the exact data we submitted in our REPL. Recall that Clojure, like all Lisps, is homoiconic. This means that your code is also data. By comparison, you cannot simply write out Java or Haskell program elements. You can write out their code's string equivalents. However, you can't do this with literal code as it lives in the runtime. Clojure has used this homoiconic quality to distill a subset of itself into a data format called the Extensible Data Notation (EDN) (you can read more at https://github.com/edn-format/edn).

EDN data values should be considered as immutable, having no notion of any program reference in a language's runtime (that is no notion of object identity). As such, this data format can be used to save data, transfer data between programs that are in different languages, and so on. EDN has its own built-in elements, such as integers, strings, lists, keywords, and so on.

There is also a facility to extend notations with tags such as (#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"). Tagged elements describe custom data types and allow you to extend the notation with your own types (hence the extensible moniker). EDN has its own built-in tagged types. This will be important later on when we want to write out the time or instance of a particular stock price (#inst "1985-04-12T23:20:50.52Z").

So, we can rightly spit out our data to a file using the .edn file extension (spit "datafile.edn" '(:more :stuff) :append true):

(defn generate-file-name [fname]
  (str "data/" fname))

(defn write-data [fname data]
  (let [full-path (generate-file-name fname)]

    (io/make-parents full-path)
    (spit full-path (list (apply pr-str data))
           :encoding "UTF-8")))

(write-data "datafile.edn" '(:one :two :three))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.221.133