Skip to content

Commit 9f02657

Browse files
Merge pull request #415 from mpenet/feature/record-ireduce
Implement IReduce on Records
2 parents 7c96df1 + 7414496 commit 9f02657

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

pixie/stdlib.pxi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,19 @@ and implements IAssociative, ILookup and IObject."
12681268
fields)
12691269
not-found#)))
12701270

1271+
'IReduce
1272+
`(-reduce [self# f# init#]
1273+
(loop [fields# ~fields
1274+
acc# init#]
1275+
(if-let [field# (first fields#)]
1276+
(let [acc# (f# acc# (map-entry field#
1277+
(get-field self#
1278+
field#)))]
1279+
(if (reduced? acc#)
1280+
@acc#
1281+
(recur (next fields#) acc#)))
1282+
acc#)))
1283+
12711284
'IMeta
12721285
`(-with-meta [self# ~meta-gs]
12731286
(new ~nm

tests/pixie/tests/test-defrecord.pxi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@
5757
(t/deftest test-record-metadata
5858
(t/assert= nil (meta t1))
5959
(t/assert= :foo (-> t1 (with-meta :foo) meta)))
60+
61+
62+
(t/deftest ireduce []
63+
(t/assert= [[:one 1] [:two 2] [:three 3]] (reduce conj [] t1))
64+
(t/assert= [1 2 3] (vals t1))
65+
(t/assert= [:one :two :three] (keys t1)))

0 commit comments

Comments
 (0)