Skip to content

Commit 97ffa10

Browse files
committed
start implementing SAM fix operators
1 parent 5f04b28 commit 97ffa10

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package io.reactivex.rxkotlin
2+
3+
import io.reactivex.Flowable
4+
import io.reactivex.functions.BiFunction
5+
import io.reactivex.functions.Function3
6+
import io.reactivex.functions.Function4
7+
import io.reactivex.functions.Function5
8+
import org.reactivestreams.Publisher
9+
10+
11+
object FlowableKt {
12+
13+
inline fun <T1,T2,R> combineLatest(source1: Flowable<T1>, source2: Flowable<T2>, crossinline combineFunction: (T1, T2) -> R) =
14+
Flowable.combineLatest(source1, source2,
15+
BiFunction<T1, T2, R> { t1, t2 -> combineFunction(t1,t2) })!!
16+
17+
inline fun <T1,T2,T3,R> combineLatest(source1: Flowable<T1>, source2: Flowable<T2>, source3: Flowable<T3>, crossinline combineFunction: (T1, T2, T3) -> R) =
18+
Flowable.combineLatest(source1, source2,source3,
19+
io.reactivex.functions.Function3<T1, T2, T3, R> { t1: T1, t2: T2, t3: T3 -> combineFunction(t1,t2, t3) })!!
20+
21+
inline fun <T1,T2,T3,T4,R> combineLatest(source1: Flowable<T1>, source2: Flowable<T2>, source3: Flowable<T3>,
22+
source4: Flowable<T4>, crossinline combineFunction: (T1, T2, T3, T4) -> R) =
23+
Flowable.combineLatest(source1, source2,source3, source4,
24+
io.reactivex.functions.Function4<T1, T2, T3, T4, R> { t1: T1, t2: T2, t3: T3, t4: T4 -> combineFunction(t1,t2, t3, t4) })!!
25+
26+
27+
inline fun <T1,T2,T3,T4,T5,R> combineLatest(source1: Flowable<T1>, source2: Flowable<T2>,
28+
source3: Flowable<T3>, source4: Flowable<T4>,
29+
source5: Flowable<T5>, crossinline combineFunction: (T1, T2, T3, T4, T5) -> R) =
30+
Flowable.combineLatest(source1, source2,source3, source4, source5,
31+
io.reactivex.functions.Function5<T1, T2, T3, T4, T5, R> { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5 -> combineFunction(t1,t2, t3, t4, t5) })!!
32+
33+
34+
35+
36+
37+
inline fun <T1,T2,R> zip(source1: Flowable<T1>, source2: Flowable<T2>, crossinline combineFunction: (T1, T2) -> R) =
38+
Flowable.zip(source1, source2,
39+
BiFunction<T1, T2, R> { t1, t2 -> combineFunction(t1,t2) })!!
40+
41+
inline fun <T1,T2,T3,R> zip(source1: Flowable<T1>, source2: Flowable<T2>, source3: Flowable<T3>, crossinline combineFunction: (T1, T2, T3) -> R) =
42+
Flowable.zip(source1, source2,source3,
43+
io.reactivex.functions.Function3<T1, T2, T3, R> { t1: T1, t2: T2, t3: T3 -> combineFunction(t1,t2, t3) })!!
44+
45+
inline fun <T1,T2,T3,T4,R> zip(source1: Flowable<T1>, source2: Flowable<T2>, source3: Flowable<T3>, source4: Flowable<T4>, crossinline combineFunction: (T1, T2, T3, T4) -> R) =
46+
Flowable.zip(source1, source2,source3, source4,
47+
io.reactivex.functions.Function4<T1, T2, T3, T4, R> { t1: T1, t2: T2, t3: T3, t4: T4 -> combineFunction(t1,t2, t3, t4) })!!
48+
49+
inline fun <T1,T2,T3,T4,T5,R> zip(source1: Flowable<T1>, source2: Flowable<T2>,
50+
source3: Flowable<T3>, source4: Flowable<T4>,
51+
source5: Flowable<T5>, crossinline combineFunction: (T1, T2, T3, T4, T5) -> R) =
52+
Flowable.zip(source1, source2,source3, source4, source5,
53+
io.reactivex.functions.Function5<T1, T2, T3, T4, T5, R> { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5 -> combineFunction(t1,t2, t3, t4, t5) })!!
54+
}
55+
56+
57+
/**
58+
* An alias to [Flowable.withLatestFrom], but allowing for cleaner lambda syntax.
59+
*/
60+
inline fun <T, U, R> Flowable<T>.withLatestFrom(other: Publisher<U>, crossinline combiner: (T, U) -> R): Flowable<R>
61+
= withLatestFrom(other, BiFunction<T, U, R> { t, u -> combiner.invoke(t, u) })
62+
63+
/**
64+
* An alias to [Flowable.withLatestFrom], but allowing for cleaner lambda syntax.
65+
*/
66+
inline fun <T, T1, T2, R> Flowable<T>.withLatestFrom(o1: Publisher<T1>, o2: Publisher<T2>, crossinline combiner: (T, T1, T2) -> R): Flowable<R>
67+
= withLatestFrom(o1, o2, Function3<T, T1, T2, R> { t, t1, t2 -> combiner.invoke(t, t1, t2) })
68+
69+
/**
70+
* An alias to [Flowable.withLatestFrom], but allowing for cleaner lambda syntax.
71+
*/
72+
inline fun <T, T1, T2, T3, R> Flowable<T>.withLatestFrom(o1: Publisher<T1>, o2: Publisher<T2>, o3: Publisher<T3>, crossinline combiner: (T, T1, T2, T3) -> R): Flowable<R>
73+
= withLatestFrom(o1, o2, o3, Function4<T, T1, T2, T3, R> { t, t1, t2, t3 -> combiner.invoke(t, t1, t2, t3) })
74+
75+
/**
76+
* An alias to [Flowable.withLatestFrom], but allowing for cleaner lambda syntax.
77+
*/
78+
inline fun <T, T1, T2, T3, T4, R> Flowable<T>.withLatestFrom(o1: Publisher<T1>, o2: Publisher<T2>, o3: Publisher<T3>, o4: Publisher<T4>, crossinline combiner: (T, T1, T2, T3, T4) -> R): Flowable<R>
79+
= withLatestFrom(o1, o2, o3, o4, Function5<T, T1, T2, T3, T4, R> { t, t1, t2, t3, t4 -> combiner.invoke(t, t1, t2, t3, t4) })
80+
81+
/**
82+
* An alias to [Flowable.zipWith], but allowing for cleaner lambda syntax.
83+
*/
84+
inline fun <T, U, R> Flowable<T>.zipWith(other: Publisher<U>, crossinline zipper: (T, U) -> R): Flowable<R>
85+
= zipWith(other, BiFunction { t, u -> zipper.invoke(t, u) })
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.reactivex.rxkotlin
2+
3+
import io.reactivex.Maybe
4+
import io.reactivex.MaybeSource
5+
import io.reactivex.functions.BiFunction
6+
7+
object MaybeKt {
8+
inline fun <T, U, R> zip(s1: MaybeSource<T>, s2: MaybeSource<U>, crossinline zipper: (T, U) -> R): MaybeSource<R>
9+
= Maybe.zip(s1,s2, BiFunction { t, u -> zipper.invoke(t, u) })
10+
}
11+
12+
/**
13+
* An alias to [Maybe.zipWith], but allowing for cleaner lambda syntax.
14+
*/
15+
inline fun <T, U, R> Maybe<T>.zipWith(other: MaybeSource<U>, crossinline zipper: (T, U) -> R): Maybe<R>
16+
= zipWith(other, BiFunction { t, u -> zipper.invoke(t, u) })
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package io.reactivex.rxkotlin
2+
3+
import io.reactivex.Observable
4+
import io.reactivex.ObservableSource
5+
import io.reactivex.functions.BiFunction
6+
7+
/**
8+
* SAM adapters to aid Kotlin lambda support
9+
*/
10+
object ObservableKt {
11+
12+
inline fun <T1,T2,R> combineLatest(source1: Observable<T1>, source2: Observable<T2>, crossinline combineFunction: (T1, T2) -> R) =
13+
Observable.combineLatest(source1, source2,
14+
BiFunction<T1, T2, R> { t1, t2 -> combineFunction(t1,t2) })!!
15+
16+
inline fun <T1,T2,T3,R> combineLatest(source1: Observable<T1>, source2: Observable<T2>, source3: Observable<T3>, crossinline combineFunction: (T1,T2, T3) -> R) =
17+
Observable.combineLatest(source1, source2,source3,
18+
io.reactivex.functions.Function3<T1, T2, T3, R> { t1: T1, t2: T2, t3: T3 -> combineFunction(t1,t2, t3) })!!
19+
20+
inline fun <T1,T2,T3,T4,R> combineLatest(source1: Observable<T1>, source2: Observable<T2>, source3: Observable<T3>,
21+
source4: Observable<T4>, crossinline combineFunction: (T1,T2, T3, T4) -> R) =
22+
Observable.combineLatest(source1, source2,source3, source4,
23+
io.reactivex.functions.Function4<T1, T2, T3, T4, R> { t1: T1, t2: T2, t3: T3, t4: T4 -> combineFunction(t1,t2, t3, t4) })!!
24+
25+
26+
inline fun <T1,T2,T3,T4,T5,R> combineLatest(source1: Observable<T1>, source2: Observable<T2>,
27+
source3: Observable<T3>, source4: Observable<T4>,
28+
source5: Observable<T5>, crossinline combineFunction: (T1,T2, T3, T4, T5) -> R) =
29+
Observable.combineLatest(source1, source2,source3, source4, source5,
30+
io.reactivex.functions.Function5<T1, T2, T3, T4, T5, R> { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5 -> combineFunction(t1,t2, t3, t4, t5) })!!
31+
32+
33+
34+
35+
36+
inline fun <T1,T2,R> zip(source1: Observable<T1>, source2: Observable<T2>, crossinline combineFunction: (T1, T2) -> R) =
37+
Observable.zip(source1, source2,
38+
BiFunction<T1, T2, R> { t1, t2 -> combineFunction(t1,t2) })!!
39+
40+
inline fun <T1,T2,T3,R> zip(source1: Observable<T1>, source2: Observable<T2>, source3: Observable<T3>, crossinline combineFunction: (T1,T2, T3) -> R) =
41+
Observable.zip(source1, source2,source3,
42+
io.reactivex.functions.Function3<T1, T2, T3, R> { t1: T1, t2: T2, t3: T3 -> combineFunction(t1,t2, t3) })!!
43+
44+
inline fun <T1,T2,T3,T4,R> zip(source1: Observable<T1>, source2: Observable<T2>, source3: Observable<T3>, source4: Observable<T4>, crossinline combineFunction: (T1,T2, T3, T4) -> R) =
45+
Observable.zip(source1, source2,source3, source4,
46+
io.reactivex.functions.Function4<T1, T2, T3, T4, R> { t1: T1, t2: T2, t3: T3, t4: T4 -> combineFunction(t1,t2, t3, t4) })!!
47+
48+
inline fun <T1,T2,T3,T4,T5,R> zip(source1: Observable<T1>, source2: Observable<T2>,
49+
source3: Observable<T3>, source4: Observable<T4>,
50+
source5: Observable<T5>, crossinline combineFunction: (T1,T2, T3, T4, T5) -> R) =
51+
Observable.zip(source1, source2,source3, source4, source5,
52+
io.reactivex.functions.Function5<T1, T2, T3, T4, T5, R> { t1: T1, t2: T2, t3: T3, t4: T4, t5: T5 -> combineFunction(t1,t2, t3, t4, t5) })!!
53+
}
54+
55+
56+
/**
57+
* An alias to [Observable.withLatestFrom], but allowing for cleaner lambda syntax.
58+
*/
59+
inline fun <T, U, R> Observable<T>.withLatestFrom(other: ObservableSource<U>, crossinline combiner: (T, U) -> R): Observable<R>
60+
= withLatestFrom(other, BiFunction<T, U, R> { t, u -> combiner.invoke(t, u) })
61+
62+
/**
63+
* An alias to [Observable.withLatestFrom], but allowing for cleaner lambda syntax.
64+
*/
65+
inline fun <T, T1, T2, R> Observable<T>.withLatestFrom(o1: ObservableSource<T1>, o2: ObservableSource<T2>, crossinline combiner: (T, T1, T2) -> R): Observable<R>
66+
= withLatestFrom(o1, o2, io.reactivex.functions.Function3<T, T1, T2, R> { t, t1, t2 -> combiner.invoke(t, t1, t2) })
67+
68+
/**
69+
* An alias to [Observable.withLatestFrom], but allowing for cleaner lambda syntax.
70+
*/
71+
inline fun <T, T1, T2, T3, R> Observable<T>.withLatestFrom(o1: ObservableSource<T1>, o2: ObservableSource<T2>, o3: ObservableSource<T3>, crossinline combiner: (T, T1, T2, T3) -> R): Observable<R>
72+
= withLatestFrom(o1, o2, o3, io.reactivex.functions.Function4<T, T1, T2, T3, R> { t, t1, t2, t3 -> combiner.invoke(t, t1, t2, t3) })
73+
74+
/**
75+
* An alias to [Observable.withLatestFrom], but allowing for cleaner lambda syntax.
76+
*/
77+
inline fun <T, T1, T2, T3, T4, R> Observable<T>.withLatestFrom(o1: ObservableSource<T1>, o2: ObservableSource<T2>, o3: ObservableSource<T3>, o4: ObservableSource<T4>, crossinline combiner: (T, T1, T2, T3, T4) -> R): Observable<R>
78+
= withLatestFrom(o1, o2, o3, o4, io.reactivex.functions.Function5<T, T1, T2, T3, T4, R> { t, t1, t2, t3, t4 -> combiner.invoke(t, t1, t2, t3, t4) })
79+
80+
/**
81+
* An alias to [Observable.zipWith], but allowing for cleaner lambda syntax.
82+
*/
83+
inline fun <T, U, R> Observable<T>.zipWith(other: ObservableSource<U>, crossinline zipper: (T, U) -> R): Observable<R>
84+
= zipWith(other, BiFunction { t, u -> zipper.invoke(t, u) })
85+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.reactivex.rxkotlin
2+
3+
import io.reactivex.Single
4+
import io.reactivex.SingleSource
5+
import io.reactivex.functions.BiFunction
6+
7+
8+
object SingleKt {
9+
inline fun <T, U, R> zip(s1: SingleSource<T>, s2: SingleSource<U>, crossinline zipper: (T, U) -> R): Single<R>
10+
= Single.zip(s1,s2, BiFunction { t, u -> zipper.invoke(t, u) })
11+
}
12+
13+
inline fun <T, U, R> Single<T>.zipWith(other: SingleSource<U>, crossinline zipper: (T, U) -> R): Single<R>
14+
= zipWith(other, BiFunction { t, u -> zipper.invoke(t, u) })

0 commit comments

Comments
 (0)