Ensure that sequences are not reordered during transform

This commit is contained in:
Joshua Suskalo 2021-08-27 16:48:33 -05:00
parent 499d1b63c0
commit 6063e1a5c1
2 changed files with 8 additions and 5 deletions

View file

@ -551,10 +551,12 @@
(let [sorted (sort-by (comp keyfn second) comparator (map-indexed vector structure)) (let [sorted (sort-by (comp keyfn second) comparator (map-indexed vector structure))
indices (map first sorted) indices (map first sorted)
result (next-fn (map second sorted)) result (next-fn (map second sorted))
unsorted (sort-by first compare (map vector (concat indices (repeat ##Inf)) result))] unsorted (sort-by first compare (map vector indices result))]
(if (seq? structure)
(doall (map second unsorted))
(into (empty structure) (into (empty structure)
(map second) (map second)
unsorted))) unsorted))))
(defn- matching-indices [aseq p] (defn- matching-indices [aseq p]
(keep-indexed (fn [i e] (if (p e) i)) aseq)) (keep-indexed (fn [i e] (if (p e) i)) aseq))

View file

@ -1709,7 +1709,8 @@
(is (= (sort initial-list) (select-one s/SORTED initial-list))) (is (= (sort initial-list) (select-one s/SORTED initial-list)))
(is (= [2 1 3 4] (transform s/SORTED reverse initial-list))) (is (= [2 1 3 4] (transform s/SORTED reverse initial-list)))
(is (= [3 2 1] (transform s/SORTED butlast initial-list))) (is (= [3 2 1] (transform s/SORTED butlast initial-list)))
(is (= [3 5 2 1] (setval [s/SORTED s/LAST] 5 initial-list)))) (is (= [3 5 2 1] (setval [s/SORTED s/LAST] 5 initial-list)))
(is (= (list 1 2 3 4 5) (transform [s/SORTED s/ALL] inc (range 5)))))
(testing "the sorted navigator with comparator" (testing "the sorted navigator with comparator"
(let [reverse-comparator (comp - compare)] (let [reverse-comparator (comp - compare)]
(is (= (sort reverse-comparator initial-list) (is (= (sort reverse-comparator initial-list)