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
142 changes: 142 additions & 0 deletions persistent.carp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ This generates:
list-ref)]
(reverse &rev)))

(doc any? "Return true if any element satisfies the predicate.")
(sig any?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all elements satisfy the predicate.")
(sig all?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for a list.")
(sig str (Fn [(Ref %name q)] String))
(defn str [list-ref]
Expand Down Expand Up @@ -409,6 +421,18 @@ This generates:
(empty)
q-ref))

(doc any? "Return true if any element satisfies the predicate.")
(sig any?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all elements satisfy the predicate.")
(sig all?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for a queue.")
(sig str (Fn [(Ref %name q)] String))
(defn str [q-ref] (fmt "(PersistentQueue length=%ld)" (length q-ref)))
Expand Down Expand Up @@ -1117,6 +1141,28 @@ This generates:
(empty)
trie-ref))

(doc any? "Return true if any entry satisfies the predicate.")
(sig any?
(Fn
[(Ref
(Fn [(Ref (Pair (Array %key-part-type) %value-type) q)] Bool)
r)
(Ref %name s)]
Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all entries satisfy the predicate.")
(sig all?
(Fn
[(Ref
(Fn [(Ref (Pair (Array %key-part-type) %value-type) q)] Bool)
r)
(Ref %name s)]
Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for a trie.")
(sig str (Fn [(Ref %name q)] String))
(defn str [trie-ref]
Expand Down Expand Up @@ -1347,6 +1393,18 @@ This generates:
(empty)
dq-ref))

(doc any? "Return true if any element satisfies the predicate.")
(sig any?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all elements satisfy the predicate.")
(sig all?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for a deque.")
(sig str (Fn [(Ref %name q)] String))
(defn str [dq-ref]
Expand Down Expand Up @@ -1850,6 +1908,24 @@ This generates:
(empty)
map-ref))

(doc any? "Return true if any entry satisfies the predicate.")
(sig any?
(Fn
[(Ref (Fn [(Ref (Pair %key-type %value-type) q)] Bool) r)
(Ref %name s)]
Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all entries satisfy the predicate.")
(sig all?
(Fn
[(Ref (Fn [(Ref (Pair %key-type %value-type) q)] Bool) r)
(Ref %name s)]
Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for an ordered map.")
(sig str (Fn [(Ref %name q)] String))
(defn str [map-ref]
Expand Down Expand Up @@ -1973,6 +2049,18 @@ Example:
ab (to-array b-ref)] (= &aa &ab))))
(implements = %name-eq)

(doc any? "Return true if any element satisfies the predicate.")
(sig any?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all elements satisfy the predicate.")
(sig all?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for an ordered set.")
(sig str (Fn [(Ref %name q)] String))
(defn str [set-ref]
Expand Down Expand Up @@ -2189,6 +2277,18 @@ Example:
(defn to-array [heap-ref]
(reduce &(fn [acc x] (Array.push-back acc x)) [] heap-ref))

(doc any? "Return true if any element satisfies the predicate.")
(sig any?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all elements satisfy the predicate.")
(sig all?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for a heap.")
(sig str (Fn [(Ref %name q)] String))
(defn str [heap-ref]
Expand Down Expand Up @@ -2514,6 +2614,24 @@ Example:
(empty)
(%map-trie map-ref)))

(doc any? "Return true if any entry satisfies the predicate.")
(sig any?
(Fn
[(Ref (Fn [(Ref (Pair %key-type %value-type) q)] Bool) r)
(Ref %name s)]
Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all entries satisfy the predicate.")
(sig all?
(Fn
[(Ref (Fn [(Ref (Pair %key-type %value-type) q)] Bool) r)
(Ref %name s)]
Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for a hash map.")
(sig str (Fn [(Ref %name q)] String))
(defn str [map-ref]
Expand Down Expand Up @@ -2631,6 +2749,18 @@ Example:
(defn = [a-ref b-ref] (%map-eq (%set-map a-ref) (%set-map b-ref)))
(implements = %name-eq)

(doc any? "Return true if any element satisfies the predicate.")
(sig any?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all elements satisfy the predicate.")
(sig all?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for a hash set.")
(sig str (Fn [(Ref %name q)] String))
(defn str [set-ref]
Expand Down Expand Up @@ -2783,6 +2913,18 @@ Example:
ab (to-array b-ref)] (= &aa &ab))))
(implements = %name-eq)

(doc any? "Return true if any element satisfies the predicate.")
(sig any?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn any? [pred coll-ref]
(reduce &(fn [acc x] (or acc (~pred &x))) false coll-ref))

(doc all? "Return true if all elements satisfy the predicate.")
(sig all?
(Fn [(Ref (Fn [(Ref %value-type q)] Bool) r) (Ref %name s)] Bool))
(defn all? [pred coll-ref]
(reduce &(fn [acc x] (and acc (~pred &x))) true coll-ref))

(doc str "Diagnostic formatting for a vector.")
(sig str (Fn [(Ref %name q)] String))
(defn str [vec-ref]
Expand Down
46 changes: 46 additions & 0 deletions test/persistent_deque.carp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,52 @@
(= &filtered &d2))
"filter with always-true preserves all elements")

(assert-equal test
true
(let [d (IntDeque.push-back 3
&(IntDeque.push-back 2
&(IntDeque.push-back 1
&(IntDeque.empty))))]
(IntDeque.any? &(fn [x] (= x &3)) &d))
"any? finds matching element")

(assert-equal test
false
(let [d (IntDeque.push-back 3
&(IntDeque.push-back 2
&(IntDeque.push-back 1
&(IntDeque.empty))))]
(IntDeque.any? &(fn [x] (= x &99)) &d))
"any? returns false when none match")

(assert-equal test
true
(let [d (IntDeque.push-back 3
&(IntDeque.push-back 2
&(IntDeque.push-back 1
&(IntDeque.empty))))]
(IntDeque.all? &(fn [x] (> @x 0)) &d))
"all? returns true when all match")

(assert-equal test
false
(let [d (IntDeque.push-back 3
&(IntDeque.push-back 2
&(IntDeque.push-back 1
&(IntDeque.empty))))]
(IntDeque.all? &(fn [x] (> @x 2)) &d))
"all? returns false when not all match")

(assert-equal test
false
(IntDeque.any? &(fn [x] (= x &1)) &(IntDeque.empty))
"any? on empty returns false")

(assert-equal test
true
(IntDeque.all? &(fn [x] (= x &1)) &(IntDeque.empty))
"all? on empty returns true")

(assert-memory-balance test
deque-branch-lifecycle
0l
Expand Down
34 changes: 34 additions & 0 deletions test/persistent_hash_map.carp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,40 @@
(Maybe.Just v) (= v 20)
(Maybe.Nothing) false)))
"merge-with on disjoint maps just takes values")

(assert-equal test
true
(let [m (IntIntMap.insert 2 5 &(IntIntMap.insert 1 10 &(IntIntMap.empty)))]
(IntIntMap.any? &(fn [p] (> @(Pair.b p) 8)) &m))
"any? finds matching entry")

(assert-equal test
false
(let [m (IntIntMap.insert 2 5 &(IntIntMap.insert 1 10 &(IntIntMap.empty)))]
(IntIntMap.any? &(fn [p] (> @(Pair.b p) 99)) &m))
"any? returns false when none match")

(assert-equal test
true
(let [m (IntIntMap.insert 2 5 &(IntIntMap.insert 1 10 &(IntIntMap.empty)))]
(IntIntMap.all? &(fn [p] (> @(Pair.b p) 0)) &m))
"all? returns true when all match")

(assert-equal test
false
(let [m (IntIntMap.insert 2 5 &(IntIntMap.insert 1 10 &(IntIntMap.empty)))]
(IntIntMap.all? &(fn [p] (> @(Pair.b p) 8)) &m))
"all? returns false when not all match")

(assert-equal test
false
(IntIntMap.any? &(fn [p] true) &(IntIntMap.empty))
"any? on empty returns false")

(assert-equal test
true
(IntIntMap.all? &(fn [p] false) &(IntIntMap.empty))
"all? on empty returns true")
(assert-memory-balance test
map-branch-lifecycle
0l
Expand Down
38 changes: 38 additions & 0 deletions test/persistent_hash_set.carp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,44 @@
(IntSet.empty? &d))
"difference with self is empty")

(assert-equal test
true
(let [s (IntSet.insert 3
&(IntSet.insert 2 &(IntSet.insert 1 &(IntSet.empty))))]
(IntSet.any? &(fn [x] (= x &3)) &s))
"any? finds matching element")

(assert-equal test
false
(let [s (IntSet.insert 3
&(IntSet.insert 2 &(IntSet.insert 1 &(IntSet.empty))))]
(IntSet.any? &(fn [x] (= x &99)) &s))
"any? returns false when none match")

(assert-equal test
true
(let [s (IntSet.insert 3
&(IntSet.insert 2 &(IntSet.insert 1 &(IntSet.empty))))]
(IntSet.all? &(fn [x] (> @x 0)) &s))
"all? returns true when all match")

(assert-equal test
false
(let [s (IntSet.insert 3
&(IntSet.insert 2 &(IntSet.insert 1 &(IntSet.empty))))]
(IntSet.all? &(fn [x] (> @x 2)) &s))
"all? returns false when not all match")

(assert-equal test
false
(IntSet.any? &(fn [x] (= x &1)) &(IntSet.empty))
"any? on empty returns false")

(assert-equal test
true
(IntSet.all? &(fn [x] (= x &1)) &(IntSet.empty))
"all? on empty returns true")

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