This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlinx.coroutines.CancellationException | |
| import kotlinx.coroutines.CoroutineExceptionHandler | |
| import kotlinx.coroutines.SupervisorJob | |
| import kotlinx.coroutines.delay | |
| import kotlinx.coroutines.launch | |
| import kotlinx.coroutines.runBlocking | |
| inline fun <R> runCatchingWithCancellation(block: () -> R): Result<R> { | |
| return try { | |
| Result.success(block()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Demonstrates inline, noinline, and crossinline in one function. | |
| * | |
| * - onSuccess: regular inline lambda - you can use 'return' to exit the calling function | |
| * - noinline onError: can be passed to other functions that expect function types | |
| * - crossinline onComplete: inline but called later (e.g., in a callback), no 'return' allowed | |
| */ | |
| inline fun String.validateEmail( | |
| onSuccess: () -> Unit, | |
| noinline onError: (String) -> Unit, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * SupervisorJob vs Regular Job - Complete Demonstration (FAST VERSION) | |
| * | |
| * This version uses shorter delays (100ms instead of 3-5 seconds) for online playgrounds | |
| * | |
| * Run in Kotlin Playground: https://play.kotlinlang.org/ | |
| */ | |
| import kotlinx.coroutines.* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun main() { | |
| runBlocking { | |
| println("=== Example 1: Cold Flow (default behavior) ===") | |
| // Cold flow: executes independently for each collector | |
| val coldFlow = flow { | |
| println("Cold Flow started") | |
| emit("A") | |
| delay(100) | |
| emit("B") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface SortingStrategy<T : Comparable<T>> { | |
| fun sort(data: List<T>): List<T> | |
| } | |
| enum class StrategyType { | |
| BUBBLE, MERGE, QUICK | |
| } | |
| class BubbleSortStrategy<T : Comparable<T>> : SortingStrategy<T> { | |
| override fun sort(data: List<T>): List<T> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class MVIViewModel<STATE, EVENT, EFFECT>( | |
| protected val savedStateHandle: SavedStateHandle? = null, | |
| protected val effectDelegate: AutoConsumableEffect<EFFECT>, | |
| ) : ViewModel(), AutoConsumableEffect<EFFECT> by effectDelegate { | |
| abstract val initialState: STATE | |
| private val _uiState by lazy { MutableStateFlow(initialState) } | |
| val uiState = _uiState.asStateFlow() |