Skip to content

Instantly share code, notes, and snippets.

@lecuseasar
Created July 12, 2022 08:29
Show Gist options
  • Save lecuseasar/68e4057f8b26edd05e9fbd687b8bc046 to your computer and use it in GitHub Desktop.
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
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