Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions persistent.carp
Original file line number Diff line number Diff line change
Expand Up @@ -3052,6 +3052,23 @@ Example:
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc map
"Apply a function to each element, returning a new vector in index order.")
(sig map
(Fn [(Ref (Fn [%value-type] %value-type) q) (Ref %name r)] %name))
(defn map [f vec-ref]
(reduce &(fn [acc x] (push-back (~f x) &acc)) (empty) vec-ref))

(doc filter
"Keep only elements satisfying a predicate, preserving index order.")
(sig filter
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] %name))
(defn filter [pred vec-ref]
(reduce
&(fn [acc x] (if (~pred &x) (push-back x &acc) acc))
(empty)
vec-ref))

(doc str "Diagnostic formatting for a vector.")
(sig str (Fn [(Ref %name q)] String))
(defn str [vec-ref]
Expand Down
52 changes: 52 additions & 0 deletions test/persistent_vector.carp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,58 @@
(IntVec.all? &(fn [x] (= x &1)) &(IntVec.empty))
"all? on empty returns true")

(assert-equal test
true
(let [v0 (IntVec.empty)
v1 (IntVec.push-back 1 &v0)
v2 (IntVec.push-back 2 &v1)
v3 (IntVec.push-back 3 &v2)
mapped (IntVec.map &(fn [x] (+ x 10)) &v3)
arr (IntVec.to-array &mapped)]
(and (= (Array.length &arr) 3)
(= @(Array.unsafe-nth &arr 0) 11)
(= @(Array.unsafe-nth &arr 1) 12)
(= @(Array.unsafe-nth &arr 2) 13)))
"map applies function preserving index order")

(assert-equal test
true
(let [v (IntVec.empty)
m (IntVec.map &(fn [x] (+ x 1)) &v)]
(IntVec.empty? &m))
"map over empty vector yields empty")

(assert-equal test
true
(let [v0 (IntVec.empty)
v1 (IntVec.push-back 1 &v0)
v2 (IntVec.push-back 2 &v1)
v3 (IntVec.push-back 3 &v2)
filtered (IntVec.filter &(fn [x] (> @x 1)) &v3)
arr (IntVec.to-array &filtered)]
(and (= (Array.length &arr) 2)
(= @(Array.unsafe-nth &arr 0) 2)
(= @(Array.unsafe-nth &arr 1) 3)))
"filter keeps elements matching predicate in order")

(assert-equal test
true
(let [v0 (IntVec.empty)
v1 (IntVec.push-back 1 &v0)
v2 (IntVec.push-back 2 &v1)
filtered (IntVec.filter &(fn [x] false) &v2)]
(IntVec.empty? &filtered))
"filter with always-false yields empty vector")

(assert-equal test
true
(let [v0 (IntVec.empty)
v1 (IntVec.push-back 1 &v0)
v2 (IntVec.push-back 2 &v1)
filtered (IntVec.filter &(fn [x] true) &v2)]
(= &filtered &v2))
"filter with always-true preserves all elements")

(assert-memory-balance test
vec-branch-lifecycle
0l
Expand Down