Skip to content

Commit 215c90d

Browse files
committed
add conj support to records (implements IPersistentCollection)
1 parent 72f112f commit 215c90d

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

pixie/stdlib.pxi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,22 @@ and implements IAssociative, ILookup and IObject."
12931293
(map #(map-entry % (get-field self# %))
12941294
~fields))
12951295

1296+
'IPersistentCollection
1297+
`(-conj [self# x]
1298+
(cond
1299+
(instance? MapEntry x)
1300+
(assoc self# (key x) (val x))
1301+
(instance? PersistentVector x)
1302+
(if (= (count x) 2)
1303+
(assoc self# (first x) (second x))
1304+
(throw
1305+
[:pixie.stdlib/InvalidArgumentException
1306+
"Vector arg to record conj must be a pair"]))))
1307+
1308+
`(-disj [self# x]
1309+
(throw [:pixie.stdlib/NotImplementedException
1310+
"disj is not supported on defrecords"]))
1311+
12961312
'IMeta
12971313
`(-with-meta [self# ~meta-gs]
12981314
(new ~nm

tests/pixie/tests/test-defrecord.pxi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
(t/assert= t t)
1515
(t/assert (satisfies? IRecord t))
1616
(t/assert (satisfies? IAssociative t))
17-
(t/assert (satisfies? ILookup t))))
17+
(t/assert (satisfies? ILookup t))
18+
(t/assert (satisfies? IPersistentCollection t))))
1819

1920
(t/deftest test-record-pred
2021
(t/assert (record? t1)))
@@ -54,6 +55,10 @@
5455
(t/assert (contains? t :two))
5556
(t/assert (contains? t :three))))
5657

58+
(t/deftest test-ipersistentcoll
59+
(t/assert= 11 (-> t1 (conj [:one 11]) :one))
60+
(t/assert= 11 (-> t1 (conj (map-entry :one 11)) :one)))
61+
5762
(t/deftest test-record-metadata
5863
(t/assert= nil (meta t1))
5964
(t/assert= :foo (-> t1 (with-meta :foo) meta)))

0 commit comments

Comments
 (0)