mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Improve the "Tagged Elements" section of EDN doc
- Change Clojure code to eliminate errors and to match commented output > Change edn/read-string to clojure.edn/read-string (no _require_ in text) > Change map->menu-item to map->MenuItem to match defrecord and output - Modify the text to make it easier to understand the given example
This commit is contained in:
parent
65d4f596cd
commit
d86597d499
@ -84,22 +84,26 @@ github/fork ; you can't eat with this
|
|||||||
|
|
||||||
#MyYelpClone/MenuItem {:name "eggs-benedict" :rating 10}
|
#MyYelpClone/MenuItem {:name "eggs-benedict" :rating 10}
|
||||||
|
|
||||||
; Let me explain this with a clojure example. Suppose I want to transform that
|
; Let me explain this with a Clojure example. Suppose I want to transform that
|
||||||
; piece of EDN into a MenuItem record.
|
; piece of EDN into a MenuItem record.
|
||||||
|
|
||||||
(defrecord MenuItem [name rating])
|
(defrecord MenuItem [name rating])
|
||||||
|
|
||||||
; To transform EDN to clojure values, I will need to use the built in EDN
|
; defrecord defined, among other things, map->MenuItem which will take a map
|
||||||
; reader, edn/read-string
|
; of field names (as keywords) to values and generate a user.MenuItem record
|
||||||
|
|
||||||
(edn/read-string "{:eggs 2 :butter 1 :flour 5}")
|
; To transform EDN to Clojure values, I will need to use the built-in EDN
|
||||||
|
; reader, clojure.edn/read-string
|
||||||
|
|
||||||
|
(clojure.edn/read-string "{:eggs 2 :butter 1 :flour 5}")
|
||||||
; -> {:eggs 2 :butter 1 :flour 5}
|
; -> {:eggs 2 :butter 1 :flour 5}
|
||||||
|
|
||||||
; To transform tagged elements, define the reader function and pass a map
|
; To transform tagged elements, pass to clojure.edn/read-string an option map
|
||||||
; that maps tags to reader functions to edn/read-string like so
|
; with a :readers map that maps tag symbols to data-reader functions, like so
|
||||||
|
|
||||||
(edn/read-string {:readers {'MyYelpClone/MenuItem map->menu-item}}
|
(clojure.edn/read-string
|
||||||
"#MyYelpClone/MenuItem {:name \"eggs-benedict\" :rating 10}")
|
{:readers {'MyYelpClone/MenuItem map->MenuItem}}
|
||||||
|
"#MyYelpClone/MenuItem {:name \"eggs-benedict\" :rating 10}")
|
||||||
; -> #user.MenuItem{:name "eggs-benedict", :rating 10}
|
; -> #user.MenuItem{:name "eggs-benedict", :rating 10}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user