kt-mvvm / com.dukescript.api.kt / computed

computed

fun <T> Provider.computed(fn: () -> T): Readable<T>

Defines derived, computed property. Its value depends on other properties and is recomputed when those dependant properties change.

public var message: String? by observable(null) {
        messageChanges++
    }
public val words: Array<String> by computed {
        val arr: Array<String> = Array(6, { "?" })
        val words: List<String> = message?.split(" ", limit = 6) ?: listOf()
        for ((index, _) in arr.withIndex()) {
            if (words.size > index) {
                arr[index] = words[index]
            }
        }
        arr
    }