apl: ∪ unique / ∪ union / ∩ intersection (+12)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 49s

- apl-unique: dedup keeping first-occurrence order
- apl-union: dedup'd A then B-elements-not-in-A
- apl-intersect: A elements that are in B, preserves left order
- ∪ wired both monadic and dyadic; ∩ wired dyadic
- pipeline 121/121
This commit is contained in:
2026-05-08 22:42:29 +00:00
parent 8cdebbe305
commit ef53232314
4 changed files with 86 additions and 1 deletions

View File

@@ -852,6 +852,40 @@
(apl-scalar (first result))
(make-array (get vals :shape) result))))))
(define
apl-unique
(fn
(arr)
(let
((ravel (if (scalar? arr) (list (disclose arr)) (get arr :ravel))))
(let
((dedup (reduce (fn (acc x) (if (index-of acc x) acc (append acc (list x)))) (list) ravel)))
(apl-vector dedup)))))
(define
apl-union
(fn
(a b)
(let
((a-ravel (if (scalar? a) (list (disclose a)) (get a :ravel)))
(b-ravel (if (scalar? b) (list (disclose b)) (get b :ravel))))
(let
((a-dedup (reduce (fn (acc x) (if (index-of acc x) acc (append acc (list x)))) (list) a-ravel)))
(let
((b-extra (filter (fn (x) (not (index-of a-dedup x))) b-ravel)))
(let
((b-extra-dedup (reduce (fn (acc x) (if (index-of acc x) acc (append acc (list x)))) (list) b-extra)))
(apl-vector (append a-dedup b-extra-dedup))))))))
(define
apl-intersect
(fn
(a b)
(let
((a-ravel (if (scalar? a) (list (disclose a)) (get a :ravel)))
(b-ravel (if (scalar? b) (list (disclose b)) (get b :ravel))))
(apl-vector (filter (fn (x) (index-of b-ravel x)) a-ravel)))))
(define
apl-primes
(fn