Skip to content

Commit 4bd54cc

Browse files
committed
data.json : use transducers for maps and sequences
1 parent 98fbd71 commit 4bd54cc

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

pixie/data/json.pxi

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
"{}"
1313
(str "{ "
1414
(->> m
15-
(map (fn [[k v]] (string/interp "$(write-string k)$: $(write-string v)$")))
16-
(string/join ", "))
15+
(transduce (comp (map (fn [[k v]] (string/interp "$(write-string k)$: $(write-string v)$")))
16+
(interpose ", "))
17+
string-builder))
1718
" }")))
1819

1920
(defn- write-sequential [xs]
2021
(if (empty? xs)
2122
"[]"
2223
(str "[ "
23-
(->> (map write-string xs)
24-
(string/join ", "))
24+
(->> xs
25+
(transduce (comp (map write-string)
26+
(interpose ", "))
27+
string-builder))
2528
" ]")))
2629

2730
(defn- write-str [s]

tests/pixie/tests/data/test-json.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
:lists {:empty {:vector [], :list '(), :seq (take 0 (range))}
1717
:non-empty {:vector [1 2 3], :list '(1 2 3), :seq (take 3 (range))}}
1818
:maps {:empty {}, :non-empty {:this "is covered, right? ;)"}}})
19-
"{ \"strings\": { \"b\": \"b\", \"c\": \"\", \"a\": \"a\", \"d\": \"d\" }, \"maps\": { \"non-empty\": { \"this\": \"is covered, right? ;)\" }, \"empty\": {} }, \"lists\": { \"non-empty\": { \"seq\": [ 0, 1, 2 ], \"list\": [ 1, 2, 3 ], \"vector\": [ 1, 2, 3 ] }, \"empty\": { \"seq\": [], \"list\": [], \"vector\": [] } }, \"numbers\": { \"one\": 1, \"two\": 2.000000, \"three\": 3 } }"))
19+
"{ \"numbers\": { \"three\": 3, \"two\": 2.000000, \"one\": 1 }, \"lists\": { \"empty\": { \"vector\": [], \"list\": [], \"seq\": [] }, \"non-empty\": { \"vector\": [ 1, 2, 3 ], \"list\": [ 1, 2, 3 ], \"seq\": [ 0, 1, 2 ] } }, \"maps\": { \"empty\": {}, \"non-empty\": { \"this\": \"is covered, right? ;)\" } }, \"strings\": { \"d\": \"d\", \"a\": \"a\", \"c\": \"\", \"b\": \"b\" } }"))
2020

2121
(deftest test-round-trip
2222
(let [data {"foo" 1, "bar" [2 3]}] ; won't work with keywords because the parser doesn't keywordise them (yet)

0 commit comments

Comments
 (0)