apl: Phase 3 grade-up ⍋ / grade-down ⍒ — 74/74 tests green
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

Add apl-grade (stable insertion sort helper), apl-grade-up, apl-grade-down.
Stability guaranteed via secondary sort key (original index). 8 new tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-06 19:02:49 +00:00
parent 03e9df3ecf
commit 7dfa092ed2
2 changed files with 85 additions and 1 deletions

View File

@@ -688,3 +688,47 @@
(map
(fn (j) (nth ravel (+ start j)))
(range 0 slice-size)))))))))))
(define
apl-grade
(fn
(arr ascending)
(let
((ravel (get arr :ravel)) (n (len (get arr :ravel))))
(let
((pairs (map (fn (i) (list (nth ravel i) (+ i apl-io))) (range 0 n))))
(define ins nil)
(set!
ins
(fn
(x sorted)
(if
(= (len sorted) 0)
(list x)
(let
((xv (first x))
(xi (nth x 1))
(hd (first sorted))
(sv (first hd))
(si (nth hd 1)))
(if
(if
ascending
(or (< xv sv) (and (= xv sv) (< xi si)))
(or (> xv sv) (and (= xv sv) (< xi si))))
(cons x sorted)
(cons hd (ins x (rest sorted))))))))
(define isort nil)
(set!
isort
(fn
(lst)
(if
(= (len lst) 0)
(list)
(ins (first lst) (isort (rest lst))))))
(make-array (list n) (map (fn (p) (nth p 1)) (isort pairs)))))))
(define apl-grade-up (fn (arr) (apl-grade arr true)))
(define apl-grade-down (fn (arr) (apl-grade arr false)))