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
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:
@@ -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)))
|
||||
|
||||
Reference in New Issue
Block a user