class DebugViewModel: ViewModel() {
private val _state: MutableStateFlow<Int> = MutableStateFlow(0)
val state: StateFlow<Int> get() = _state
fun calculateFactorialOf(n: Int) {
viewModelScope.launch {
var result = 1
repeat(n) { i ->
result = calculate(result, i)
}
_state.emit(result)
}
}
private fun calculate(res: Int, i: Int): Int = res * i
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter