Skip to content

Instantly share code, notes, and snippets.

View hungyanbin's full-sized avatar

HUNG YANBIN hungyanbin

View GitHub Profile
@hungyanbin
hungyanbin / .kt
Created October 28, 2026 06:57
RxJava Gist
fun <IN, OUT> Observable<IN>.filterMap(f: (IN) -> OUT?): Observable<OUT> {
class Wrapper(val inner: OUT?)
return this
.map { Wrapper(f(it)) }
.filter { it.inner != null }
.map { it.inner }
}
private fun ResourcerManager.getScalableCBImageTask(
fun <IN, OUT> Observable<IN>.filterMap(f: (IN) -> OUT?): Observable<OUT> {
class Wrapper(val inner: OUT?)
return this
.map { Wrapper(f(it)) }
.filter { it.inner != null }
.map { it.inner }
}
private fun ResourcerManager.getScalableCBImageTask(
package com.yanbin.myapplication
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.Typeface
import android.text.TextPaint
import android.util.AttributeSet
import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
context(Context)
fun alertDialog2(execute: context(AlertDialogBuilder2) () -> Unit): AlertDialog {
val builder = AlertDialogBuilder2().also(execute)
return builder.build()
}
import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
fun Context.alertDialog(execute: AlertDialogBuilder.() -> Unit): AlertDialog {
val builder = AlertDialogBuilder()
builder.execute()
return builder.build(this)
}
class StreamTest {
// Reactive programming is basically producer-consumer pattern
@Test
fun `producer consumer`() {
val producer = Observable.create<Int> { emitter ->
for (i in 0..5) {
println("produce $i")
emitter.onNext(i)
}
import androidx.compose.animation.core.*
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathEffect
@Test
fun parallelTest() {
val subject = PublishSubject.create<Long>()
subject.subscribe {
println("subject.subscribe: ${it}, thread: ${Thread.currentThread().name}")
}
class ParallelSubscriber: Subscriber<Long> {
override fun onSubscribe(s: Subscription) {
s.request(1000)
}
class CountDown {
fun execute(number: Int): Int {
// (351, 2) -> 3
// (351, 1) -> 5
// (351, 0) -> 1
val digitExtractor: (Int, Int) -> Int = { number, digit ->
if (number < 10 ) {
number
} else {
class InMemoryNoteRepository: NoteRepository {
private val notes = BehaviorSubject.createDefault(emptyList<Note>())
private val noteMap = ConcurrentHashMap<String, Note>()
override fun getAllNotes(): Observable<List<Note>> = notes.hide()
override fun putNote(note: Note) {
noteMap[note.id] = note
notes.onNext(noteMap.elements().toList())