Skip to content

Commit ee45b44

Browse files
committed
Eliminate sorted-list variants and cleanup sort tests
1 parent ea22e7a commit ee45b44

File tree

2 files changed

+32
-61
lines changed
  • language-adaptors/rxjava-clojure/src

2 files changed

+32
-61
lines changed

language-adaptors/rxjava-clojure/src/main/clojure/rx/lang/clojure/core.clj

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -681,36 +681,7 @@
681681
(filter identity)
682682
first))
683683

684-
(defn sorted-list
685-
"Returns an observable that emits a *single value* which is a sorted List
686-
of the items in coll, where the sort order is determined by comparing
687-
items. If no comparator is supplied, uses compare. comparator must
688-
implement java.util.Comparator.
689-
690-
Use sort if you don't want the sequence squashed down to a List.
691-
692-
See:
693-
rx.Observable/toSortedList
694-
sort
695-
"
696-
([coll] (sorted-list clojure.core/compare coll))
697-
([comp ^Observable coll]
698-
(.toSortedList coll (iop/fn [a b]
699-
; force to int so rxjava doesn't have a fit
700-
(int (comp a b))))))
701-
702-
(defn sorted-list-by
703-
"Returns an observable that emits a *single value* which is a sorted List
704-
of the items in coll, where the sort order is determined by comparing
705-
(keyfn item). If no comparator is supplied, uses compare. comparator must
706-
implement java.util.Comparator.
707-
708-
Use sort-by if you don't want the sequence squashed down to a List.
709-
710-
See:
711-
rx.Observable/toSortedList
712-
sort-by
713-
"
684+
(defn ^:private sorted-list-by
714685
([keyfn coll] (sorted-list-by keyfn clojure.core/compare coll))
715686
([keyfn comp ^Observable coll]
716687
(.toSortedList coll (iop/fn [a b]
@@ -723,16 +694,13 @@
723694
comparator must implement java.util.Comparator.
724695
725696
See:
726-
sorted-list
727697
clojure.core/sort
728698
"
729699
([xs]
730-
(->> xs
731-
(sorted-list)
732-
(mapcat seq->o)))
700+
(sort clojure.core/compare xs))
733701
([comp xs]
734702
(->> xs
735-
(sorted-list comp)
703+
(sorted-list-by identity comp)
736704
(mapcat seq->o))))
737705

738706
(defn sort-by
@@ -744,8 +712,7 @@
744712
clojure.core/sort-by
745713
"
746714
([keyfn xs]
747-
(->> (sorted-list-by keyfn xs)
748-
(mapcat seq->o)))
715+
(sort-by keyfn clojure.core/compare xs))
749716
([keyfn comp ^Observable xs]
750717
(->> xs
751718
(sorted-list-by keyfn comp)

language-adaptors/rxjava-clojure/src/test/clojure/rx/lang/clojure/core_test.clj

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -480,33 +480,37 @@
480480
(is (= [:r] (b/into [] (rx/some #{:r :s :t} (rx/seq->o [:q :v :r])))))
481481
(is (= [] (b/into [] (rx/some #{:r :s :t} (rx/seq->o [:q :v]))))))
482482

483-
(deftest test-sorted-list
484-
(is (= [[]] (b/into [] (rx/sorted-list (rx/empty)))))
485-
(is (= [[1 2 3]]
486-
(b/into [] (rx/sorted-list (rx/seq->o [3 1 2])))))
487-
(is (= [[3 2 1]]
488-
(b/into [] (rx/sorted-list (fn [a b] (- (compare a b))) (rx/seq->o [2 1 3]))))))
489-
490-
(deftest test-sorted-list-by
491-
(is (= [[]] (b/into [] (rx/sorted-list-by :foo (rx/empty)))))
492-
(is (= [[{:foo 1} {:foo 2} {:foo 3}]]
493-
(b/into [] (rx/sorted-list-by :foo (rx/seq->o [{:foo 2}{:foo 1}{:foo 3}])))))
494-
(is (= [[{:foo 3} {:foo 2} {:foo 1}]]
495-
(b/into [] (rx/sorted-list-by :foo (fn [a b] (- (compare a b))) (rx/seq->o [{:foo 2}{:foo 1}{:foo 3}]))))))
496-
497483
(deftest test-sort
498-
(is (= [] (b/into [] (rx/sort (rx/empty)))))
499-
(is (= [1 2 3]
500-
(b/into [] (rx/sort (rx/seq->o [3 1 2])))))
501-
(is (= [3 2 1]
502-
(b/into [] (rx/sort (fn [a b] (- (compare a b))) (rx/seq->o [2 1 3]))))))
484+
(are [in cmp] (= (if cmp
485+
(sort cmp in)
486+
(sort in))
487+
(->> in
488+
(rx/seq->o)
489+
(#(if cmp (rx/sort cmp %) (rx/sort %)))
490+
(b/into [])))
491+
[] nil
492+
[] (comp - compare)
493+
[3 1 2] nil
494+
[1 2 3] nil
495+
[1 2 3] (comp - compare)
496+
[2 1 3] (comp - compare)))
503497

504498
(deftest test-sort-by
505-
(is (= [] (b/into [] (rx/sort-by :foo (rx/empty)))))
506-
(is (= [{:foo 1} {:foo 2} {:foo 3}]
507-
(b/into [] (rx/sort-by :foo (rx/seq->o [{:foo 2}{:foo 1}{:foo 3}])))))
508-
(is (= [{:foo 3} {:foo 2} {:foo 1}]
509-
(b/into [] (rx/sort-by :foo (fn [a b] (- (compare a b))) (rx/seq->o [{:foo 2}{:foo 1}{:foo 3}]))))))
499+
(are [rin cmp] (let [in (map #(hash-map :foo %) rin)]
500+
(= (if cmp
501+
(sort-by :foo cmp in)
502+
(sort-by :foo in))
503+
(->> in
504+
(rx/seq->o)
505+
(#(if cmp (rx/sort-by :foo cmp %) (rx/sort-by :foo %)))
506+
(b/into []))))
507+
[] nil
508+
[] (comp - compare)
509+
[3 1 2] nil
510+
[1 2 3] nil
511+
[1 2 3] (comp - compare)
512+
[2 1 3] (comp - compare)))
513+
510514

511515
(deftest test-split-with
512516
(is (= (split-with (partial >= 3) (range 6))

0 commit comments

Comments
 (0)