;; The shape prelude.ml's sort-i32-by! / sort-f32-by! pair would collapse into: ;; one generic body, the comparison passed in as a function value because an ;; unconstrained type variable has no < of its own. (defn swap! [xs [$t] i i32 j i32] () (let [tmp (at xs i)] (set (at xs i) (at xs j)) (set (at xs j) tmp))) (defn sort-by! [s [$t] before? (Fn [$t $t] bool)] () (let [i 1] (while (< i (len s)) (let [j i] (while (and (> j 0) (before? (at s j) (at s (- j 1)))) (swap! s (- j 1) j) (set j (- j 1)))) (set i (+ i 1))))) (defn main [] () (let [ns [5 3 9 1] fs [2.5 0.5 1.5]] (sort-by! (slice ns 0 4) (fn [a b] (< a b))) (sort-by! (slice fs 0 3) (fn [a b] (> a b))) (dotimes [i 4] (println (at ns i))) (dotimes [i 3] (println (at fs i)))))