Skip to content

Commit 02ff779

Browse files
committed
Implemented partition-all
There isn't really an rxjava impl equivalent to clojure.core/partition, so it's omitted.
1 parent 3714db9 commit 02ff779

File tree

2 files changed

+39
-2
lines changed
  • language-adaptors/rxjava-clojure/src

2 files changed

+39
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
interleave interpose into
88
keep keep-indexed
99
map mapcat map-indexed
10-
merge next nth partition reduce reductions
10+
merge next nth partition-all reduce reductions
1111
rest seq some sort sort-by split-with
1212
take take-while throw])
1313
(:require [rx.lang.clojure.interop :as iop]
@@ -618,7 +618,17 @@
618618
([^Observable xs index not-found]
619619
(.elementAtOrDefault xs index not-found)))
620620

621-
; TODO partition. Use window
621+
(defn ^Observable partition-all
622+
"Returns an Observable of Observables of n items each, at offsets step
623+
apart. If step is not supplied, defaults to n, i.e. the partitions
624+
do not overlap. May include partitions with fewer than n items at the end.
625+
626+
See:
627+
clojure.core/partition-all
628+
rx.Observable/window
629+
"
630+
([n ^Observable xs] (.window xs (int n)))
631+
([n step ^Observable xs] (.window xs (int n) (int step))))
622632

623633
(defn ^Observable reduce
624634
([f ^Observable xs] (.reduce xs (iop/fn* f)))

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,33 @@
416416
(let [in [:q :r :s :t :u]]
417417
(is (= (rest in) (b/into [] (rx/rest (rx/seq->o in)))))))
418418

419+
(deftest test-partition-all
420+
(are [input-size part-size step] (= (->> (range input-size)
421+
(partition-all part-size step))
422+
(->> (range input-size)
423+
(rx/seq->o)
424+
(rx/partition-all part-size step)
425+
(rx/map #(rx/into [] %))
426+
(rx/concat*)
427+
(b/into [])))
428+
0 1 1
429+
10 2 2
430+
10 3 2
431+
15 30 4)
432+
433+
(are [input-size part-size] (= (->> (range input-size)
434+
(partition-all part-size))
435+
(->> (range input-size)
436+
(rx/seq->o)
437+
(rx/partition-all part-size)
438+
(rx/map #(rx/into [] %))
439+
(rx/concat*)
440+
(b/into [])))
441+
0 1
442+
10 2
443+
10 3
444+
15 30))
445+
419446
(deftest test-reduce
420447
(is (= (reduce + 0 (range 4))
421448
(b/first (rx/reduce + 0 (rx/seq->o (range 4)))))))

0 commit comments

Comments
 (0)