From af3e161a2e1eadaec8067c98fbbfbbbbdf89b533 Mon Sep 17 00:00:00 2001 From: "carpentry-heartbeat[bot]" Date: Sun, 24 May 2026 02:10:33 +0200 Subject: [PATCH] Add any? and all? predicate combinators to all 10 data structures --- persistent.carp | 142 ++++++++++++++++++++++++++++++++++ test/persistent_deque.carp | 46 +++++++++++ test/persistent_hash_map.carp | 34 ++++++++ test/persistent_hash_set.carp | 38 +++++++++ test/persistent_heap.carp | 70 ++++++++++++----- test/persistent_list.carp | 46 +++++++++++ test/persistent_ord_map.carp | 34 ++++++++ test/persistent_ord_set.carp | 38 +++++++++ test/persistent_queue.carp | 46 +++++++++++ test/persistent_trie.carp | 50 ++++++++++++ test/persistent_vector.carp | 84 +++++++++++++++----- 11 files changed, 589 insertions(+), 39 deletions(-) diff --git a/persistent.carp b/persistent.carp index 2e7e01e..56b8e7f 100644 --- a/persistent.carp +++ b/persistent.carp @@ -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] @@ -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))) @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] diff --git a/test/persistent_deque.carp b/test/persistent_deque.carp index bf4790a..9372f29 100644 --- a/test/persistent_deque.carp +++ b/test/persistent_deque.carp @@ -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 diff --git a/test/persistent_hash_map.carp b/test/persistent_hash_map.carp index 47e77aa..00e1d9e 100644 --- a/test/persistent_hash_map.carp +++ b/test/persistent_hash_map.carp @@ -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 diff --git a/test/persistent_hash_set.carp b/test/persistent_hash_set.carp index 8b12478..8ccdafe 100644 --- a/test/persistent_hash_set.carp +++ b/test/persistent_hash_set.carp @@ -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 diff --git a/test/persistent_heap.carp b/test/persistent_heap.carp index 916bdf0..1beced0 100644 --- a/test/persistent_heap.carp +++ b/test/persistent_heap.carp @@ -15,8 +15,7 @@ (defn build-descending-heap [n] (let-do [h (IntHeap.empty)] - (for [i 0 n] - (set! h (IntHeap.insert (- n i) &h))) + (for [i 0 n] (set! h (IntHeap.insert (- n i) &h))) h)) (defn heap-branch-lifecycle [] @@ -27,9 +26,7 @@ h4 (IntHeap.merge &h2 &h3)] (ignore h4))) -(defn pop-values [heap-ref] - (let [out0 []] - (pop-values-iter heap-ref &out0))) +(defn pop-values [heap-ref] (let [out0 []] (pop-values-iter heap-ref &out0))) (defn pop-values-iter [heap-ref out-ref] (match (IntHeap.pop heap-ref) @@ -37,15 +34,12 @@ (Maybe.Just p) (let [value @(Pair.a &p) next-h @(Pair.b &p)] - (do - (Array.push-back! out-ref value) - (pop-values-iter &next-h out-ref))))) + (do (Array.push-back! out-ref value) (pop-values-iter &next-h out-ref))))) (deftest test (assert-equal test true - (let [h (IntHeap.empty)] - (IntHeap.empty? &h)) + (let [h (IntHeap.empty)] (IntHeap.empty? &h)) "empty heap reports empty") (assert-equal test @@ -54,9 +48,7 @@ h1 (IntHeap.insert 5 &h0) h2 (IntHeap.insert 2 &h1) h3 (IntHeap.insert 7 &h2)] - (match (IntHeap.peek &h3) - (Maybe.Just v) (= v 2) - (Maybe.Nothing) false)) + (match (IntHeap.peek &h3) (Maybe.Just v) (= v 2) (Maybe.Nothing) false)) "peek returns minimum") (assert-equal test @@ -91,9 +83,7 @@ true (let [h (build-descending-heap 100000)] (and (= (IntHeap.length &h) 100000l) - (match (IntHeap.peek &h) - (Maybe.Just v) (= v 1) - (Maybe.Nothing) false))) + (match (IntHeap.peek &h) (Maybe.Just v) (= v 1) (Maybe.Nothing) false))) "descending insert of 100k items does not blow the stack via merge-roots") (assert-equal test @@ -115,5 +105,49 @@ (= (Array.length &arr) 3)) "to-array has count elements") - (assert-memory-balance test heap-branch-lifecycle 0l "persistent heap branch lifecycle is memory-balanced") -) + (assert-equal test + true + (let [h (IntHeap.insert 3 + &(IntHeap.insert 2 + &(IntHeap.insert 1 &(IntHeap.empty))))] + (IntHeap.any? &(fn [x] (= x &3)) &h)) + "any? finds matching element") + + (assert-equal test + false + (let [h (IntHeap.insert 3 + &(IntHeap.insert 2 + &(IntHeap.insert 1 &(IntHeap.empty))))] + (IntHeap.any? &(fn [x] (= x &99)) &h)) + "any? returns false when none match") + + (assert-equal test + true + (let [h (IntHeap.insert 3 + &(IntHeap.insert 2 + &(IntHeap.insert 1 &(IntHeap.empty))))] + (IntHeap.all? &(fn [x] (> @x 0)) &h)) + "all? returns true when all match") + + (assert-equal test + false + (let [h (IntHeap.insert 3 + &(IntHeap.insert 2 + &(IntHeap.insert 1 &(IntHeap.empty))))] + (IntHeap.all? &(fn [x] (> @x 2)) &h)) + "all? returns false when not all match") + + (assert-equal test + false + (IntHeap.any? &(fn [x] (= x &1)) &(IntHeap.empty)) + "any? on empty returns false") + + (assert-equal test + true + (IntHeap.all? &(fn [x] (= x &1)) &(IntHeap.empty)) + "all? on empty returns true") + + (assert-memory-balance test + heap-branch-lifecycle + 0l + "persistent heap branch lifecycle is memory-balanced")) diff --git a/test/persistent_list.carp b/test/persistent_list.carp index c331b60..dcd3570 100644 --- a/test/persistent_list.carp +++ b/test/persistent_list.carp @@ -206,6 +206,52 @@ (= &filtered &l2)) "filter with always-true preserves all elements") + (assert-equal test + true + (let [l0 (IntList.empty) + l1 (IntList.prepend 1 &l0) + l2 (IntList.prepend 2 &l1) + l3 (IntList.prepend 3 &l2)] + (IntList.any? &(fn [x] (= x &3)) &l3)) + "any? finds matching element") + + (assert-equal test + false + (let [l0 (IntList.empty) + l1 (IntList.prepend 1 &l0) + l2 (IntList.prepend 2 &l1) + l3 (IntList.prepend 3 &l2)] + (IntList.any? &(fn [x] (= x &99)) &l3)) + "any? returns false when none match") + + (assert-equal test + true + (let [l0 (IntList.empty) + l1 (IntList.prepend 1 &l0) + l2 (IntList.prepend 2 &l1) + l3 (IntList.prepend 3 &l2)] + (IntList.all? &(fn [x] (> @x 0)) &l3)) + "all? returns true when all match") + + (assert-equal test + false + (let [l0 (IntList.empty) + l1 (IntList.prepend 1 &l0) + l2 (IntList.prepend 2 &l1) + l3 (IntList.prepend 3 &l2)] + (IntList.all? &(fn [x] (> @x 2)) &l3)) + "all? returns false when not all match") + + (assert-equal test + false + (IntList.any? &(fn [x] (= x &1)) &(IntList.empty)) + "any? on empty returns false") + + (assert-equal test + true + (IntList.all? &(fn [x] (= x &1)) &(IntList.empty)) + "all? on empty returns true") + (assert-memory-balance test list-branch-lifecycle 0l diff --git a/test/persistent_ord_map.carp b/test/persistent_ord_map.carp index 62f3d0b..b84062f 100644 --- a/test/persistent_ord_map.carp +++ b/test/persistent_ord_map.carp @@ -281,6 +281,40 @@ (Maybe.Nothing) false))) "merge-with on disjoint maps just takes values") + (assert-equal test + true + (let [m (IntIntOMap.insert 2 5 &(IntIntOMap.insert 1 10 &(IntIntOMap.empty)))] + (IntIntOMap.any? &(fn [p] (> @(Pair.b p) 8)) &m)) + "any? finds matching entry") + + (assert-equal test + false + (let [m (IntIntOMap.insert 2 5 &(IntIntOMap.insert 1 10 &(IntIntOMap.empty)))] + (IntIntOMap.any? &(fn [p] (> @(Pair.b p) 99)) &m)) + "any? returns false when none match") + + (assert-equal test + true + (let [m (IntIntOMap.insert 2 5 &(IntIntOMap.insert 1 10 &(IntIntOMap.empty)))] + (IntIntOMap.all? &(fn [p] (> @(Pair.b p) 0)) &m)) + "all? returns true when all match") + + (assert-equal test + false + (let [m (IntIntOMap.insert 2 5 &(IntIntOMap.insert 1 10 &(IntIntOMap.empty)))] + (IntIntOMap.all? &(fn [p] (> @(Pair.b p) 8)) &m)) + "all? returns false when not all match") + + (assert-equal test + false + (IntIntOMap.any? &(fn [p] true) &(IntIntOMap.empty)) + "any? on empty returns false") + + (assert-equal test + true + (IntIntOMap.all? &(fn [p] false) &(IntIntOMap.empty)) + "all? on empty returns true") + (assert-memory-balance test map-branch-lifecycle 0l diff --git a/test/persistent_ord_set.carp b/test/persistent_ord_set.carp index e4dae0e..0d75c64 100644 --- a/test/persistent_ord_set.carp +++ b/test/persistent_ord_set.carp @@ -138,6 +138,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 diff --git a/test/persistent_queue.carp b/test/persistent_queue.carp index fa658c0..cafd3fe 100644 --- a/test/persistent_queue.carp +++ b/test/persistent_queue.carp @@ -199,6 +199,52 @@ (= &filtered &q2)) "filter with always-true preserves all elements") + (assert-equal test + true + (let [q (IntQueue.enqueue 3 + &(IntQueue.enqueue 2 + &(IntQueue.enqueue 1 + &(IntQueue.empty))))] + (IntQueue.any? &(fn [x] (= x &3)) &q)) + "any? finds matching element") + + (assert-equal test + false + (let [q (IntQueue.enqueue 3 + &(IntQueue.enqueue 2 + &(IntQueue.enqueue 1 + &(IntQueue.empty))))] + (IntQueue.any? &(fn [x] (= x &99)) &q)) + "any? returns false when none match") + + (assert-equal test + true + (let [q (IntQueue.enqueue 3 + &(IntQueue.enqueue 2 + &(IntQueue.enqueue 1 + &(IntQueue.empty))))] + (IntQueue.all? &(fn [x] (> @x 0)) &q)) + "all? returns true when all match") + + (assert-equal test + false + (let [q (IntQueue.enqueue 3 + &(IntQueue.enqueue 2 + &(IntQueue.enqueue 1 + &(IntQueue.empty))))] + (IntQueue.all? &(fn [x] (> @x 2)) &q)) + "all? returns false when not all match") + + (assert-equal test + false + (IntQueue.any? &(fn [x] (= x &1)) &(IntQueue.empty)) + "any? on empty returns false") + + (assert-equal test + true + (IntQueue.all? &(fn [x] (= x &1)) &(IntQueue.empty)) + "all? on empty returns true") + (assert-memory-balance test queue-branch-lifecycle 0l diff --git a/test/persistent_trie.carp b/test/persistent_trie.carp index 5557ea8..1e1c1b7 100644 --- a/test/persistent_trie.carp +++ b/test/persistent_trie.carp @@ -223,6 +223,56 @@ (IntTrieInt.empty? &filtered)) "filter with always-false yields empty trie") + (assert-equal test + true + (let [k1 [1 2] + k2 [3 4] + t0 (IntTrieInt.empty) + t1 (IntTrieInt.insert &k1 10 &t0) + t2 (IntTrieInt.insert &k2 5 &t1)] + (IntTrieInt.any? &(fn [p] (> @(Pair.b p) 8)) &t2)) + "any? finds matching entry") + + (assert-equal test + false + (let [k1 [1 2] + k2 [3 4] + t0 (IntTrieInt.empty) + t1 (IntTrieInt.insert &k1 10 &t0) + t2 (IntTrieInt.insert &k2 5 &t1)] + (IntTrieInt.any? &(fn [p] (> @(Pair.b p) 99)) &t2)) + "any? returns false when none match") + + (assert-equal test + true + (let [k1 [1 2] + k2 [3 4] + t0 (IntTrieInt.empty) + t1 (IntTrieInt.insert &k1 10 &t0) + t2 (IntTrieInt.insert &k2 5 &t1)] + (IntTrieInt.all? &(fn [p] (> @(Pair.b p) 0)) &t2)) + "all? returns true when all match") + + (assert-equal test + false + (let [k1 [1 2] + k2 [3 4] + t0 (IntTrieInt.empty) + t1 (IntTrieInt.insert &k1 10 &t0) + t2 (IntTrieInt.insert &k2 5 &t1)] + (IntTrieInt.all? &(fn [p] (> @(Pair.b p) 8)) &t2)) + "all? returns false when not all match") + + (assert-equal test + false + (IntTrieInt.any? &(fn [p] true) &(IntTrieInt.empty)) + "any? on empty returns false") + + (assert-equal test + true + (IntTrieInt.all? &(fn [p] false) &(IntTrieInt.empty)) + "all? on empty returns true") + (assert-memory-balance test trie-branch-lifecycle 0l diff --git a/test/persistent_vector.carp b/test/persistent_vector.carp index 976eeb2..10b8453 100644 --- a/test/persistent_vector.carp +++ b/test/persistent_vector.carp @@ -18,21 +18,16 @@ v1 (IntVec.push-back 1 &v0) v2 (IntVec.push-back 2 &v1) v3 (IntVec.push-back 3 &v1) - v4 (match (IntVec.assoc 0 9 &v2) - (Maybe.Just v) v - (Maybe.Nothing) v2) + v4 (match (IntVec.assoc 0 9 &v2) (Maybe.Just v) v (Maybe.Nothing) v2) v5 (match (IntVec.pop-back &v4) - (Maybe.Just p) @(Pair.b &p) - (Maybe.Nothing) v4)] - (do - (ignore v3) - (ignore v5)))) + (Maybe.Just p) @(Pair.b &p) + (Maybe.Nothing) v4)] + (do (ignore v3) (ignore v5)))) (deftest test (assert-equal test true - (let [v (IntVec.empty)] - (IntVec.empty? &v)) + (let [v (IntVec.empty)] (IntVec.empty? &v)) "empty vector reports empty") (assert-equal test @@ -56,15 +51,14 @@ v1 (IntVec.push-back 10 &v0) v2 (IntVec.push-back 20 &v1) assoc-res (IntVec.assoc 1 99 &v2)] - (and (match assoc-res - (Maybe.Just v) - (match (IntVec.get 1 &v) - (Maybe.Just x) (= x 99) - (Maybe.Nothing) false) - (Maybe.Nothing) false) - (match (IntVec.get 1 &v2) - (Maybe.Just v) (= v 20) - (Maybe.Nothing) false))) + (and + (match assoc-res + (Maybe.Just v) + (match (IntVec.get 1 &v) + (Maybe.Just x) (= x 99) + (Maybe.Nothing) false) + (Maybe.Nothing) false) + (match (IntVec.get 1 &v2) (Maybe.Just v) (= v 20) (Maybe.Nothing) false))) "assoc updates index while preserving prior version") (assert-equal test @@ -112,5 +106,53 @@ (and (= &a &b) (not (= &a &c)))) "= is pairwise on index-ordered values") - (assert-memory-balance test vec-branch-lifecycle 0l "persistent vector branch lifecycle is memory-balanced") -) + (assert-equal test + true + (let [v (IntVec.push-back 3 + &(IntVec.push-back 2 + &(IntVec.push-back 1 + &(IntVec.empty))))] + (IntVec.any? &(fn [x] (= x &3)) &v)) + "any? finds matching element") + + (assert-equal test + false + (let [v (IntVec.push-back 3 + &(IntVec.push-back 2 + &(IntVec.push-back 1 + &(IntVec.empty))))] + (IntVec.any? &(fn [x] (= x &99)) &v)) + "any? returns false when none match") + + (assert-equal test + true + (let [v (IntVec.push-back 3 + &(IntVec.push-back 2 + &(IntVec.push-back 1 + &(IntVec.empty))))] + (IntVec.all? &(fn [x] (> @x 0)) &v)) + "all? returns true when all match") + + (assert-equal test + false + (let [v (IntVec.push-back 3 + &(IntVec.push-back 2 + &(IntVec.push-back 1 + &(IntVec.empty))))] + (IntVec.all? &(fn [x] (> @x 2)) &v)) + "all? returns false when not all match") + + (assert-equal test + false + (IntVec.any? &(fn [x] (= x &1)) &(IntVec.empty)) + "any? on empty returns false") + + (assert-equal test + true + (IntVec.all? &(fn [x] (= x &1)) &(IntVec.empty)) + "all? on empty returns true") + + (assert-memory-balance test + vec-branch-lifecycle + 0l + "persistent vector branch lifecycle is memory-balanced"))