Skip to content

Instantly share code, notes, and snippets.

@gbajaj
gbajaj / kt
Created July 2, 2025 05:12
flatMapLatest
fun <T, R> Flow<T>.flatMapLatest(transform: suspend (T) -> Flow<R>): Flow<R> = flow {
// This coroutineScope is key to managing cancellation of the inner flow
coroutineScope {
var previousJob: Job? = null
collect { value ->
// Cancel any ongoing inner flow collection
previousJob?.cancelAndJoin()
// Launch new inner flow collector in a new coroutine
@gbajaj
gbajaj / .kt
Created July 2, 2025 05:01
DistinctUntilChanged
fun <T> Flow<T>.distinctUntilChanged(comparator: (T, T) -> Boolean): Flow<T> = flow {
var previous: T? = null
var isFirst = true
collect { value ->
if (isFirst || !comparator(previous!!, value)) {
emit(value)
previous = value
isFirst = false
}
import kotlinx.coroutines.*
class Debouncer(private val delayMs: Long = 300) {
private var debounceJob: Job? = null
fun submit(scope: CoroutineScope, action: () -> Unit) {
debounceJob?.cancel()
debounceJob = scope.launch {
delay(delayMs)
action()
@gbajaj
gbajaj / codeFile.kt
Created June 5, 2025 22:13
Test Gist for Obsidian
fun testGist() {
println("Hello world!")
}
@gbajaj
gbajaj / cs
Created June 15, 2021 23:20
Strart
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
@gbajaj
gbajaj / CancelCheckInDoInBackground.java
Created July 9, 2019 19:35
DoInBackgroundCancel.java
DoInBackground(. . . ) {
//Doing some work
if (isCancelled()) {
// we are cancelled, no need to do any work now
. . .
// Did we override implement onCancelled() or onCancelled(Object)?
}
for (CurrentWork : ListOfWorks) {
if (isCancelled()) {
// we are cancelled, no need to do any work now
@gbajaj
gbajaj / rightIntegerComparison.java
Created July 8, 2019 20:42
Comparing Integers Right Way
public class Main {
public static void main(String[] args) {
Integer a = new Integer(10);
Integer b = new Integer(10);
if (a.equals(b)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
@gbajaj
gbajaj / IntegerComparison.java
Created July 8, 2019 20:23
Compare Integers with Objects
public class Main {
public static void main(String[] args) {
Integer a = new Integer(10);
Integer b= new Integer(10);
if (a == b) {
System.out.println("true");
} else {
System.out.println("false");
}
}
@gbajaj
gbajaj / intCompare.java
Created July 8, 2019 20:16
Java Simple Integer Comparison
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 10;
if (a == b) {
System.out.println("true");
} else {
System.out.println("false");
}
}
@gbajaj
gbajaj / gist:b9917610470d9cb75e66fc65d155cf60
Created July 8, 2019 20:15
Java Simple Integer Comparison
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 10;
if (a == b) {
System.out.println("true");
} else {
System.out.println("false");
}
}