Created
July 12, 2022 08:29
-
-
Save lecuseasar/68e4057f8b26edd05e9fbd687b8bc046 to your computer and use it in GitHub Desktop.
lambdas and higher-order functionshttps://developer.android.com/codelabs/kotlin-bootcamp-functions#6
This file contains 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() { | |
var dirtyLevel = 30 | |
val waterFilter: (Int) -> Int = { dirty: Int -> dirty / 2 } | |
println(waterFilter(dirtyLevel)) | |
println(updateDirty(dirtyLevel, waterFilter)) | |
println(updateDirty(dirtyLevel, ::increaseDirty)) | |
dirtyLevel = updateDirty(dirtyLevel) { it + 23 } | |
println(dirtyLevel) | |
} | |
fun updateDirty(dirty: Int, operation: (Int) -> Int): Int = operation(dirty) | |
fun increaseDirty(start: Int) = start + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment