Lambda Expression in Kotlin
Level 3109 keystrokes
Stores lambda expressions in variables and calls them like functions.
Kotlin code
// A lambda is written { parameters -> body }; this one doubles x// The last expression (x * 2) is the return value1val double = { x: Int -> x * 2 }// A lambda with two Int parameters that returns their sum2val add = { a: Int, b: Int -> a + b }3 // Call the lambda like a function: 5 * 2 prints 104println(double(5))// 3 + 4 prints 75println(add(3, 4))Output
10 7
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Kotlin code and how often they appear.
(× 4)× 4:× 3-× 2,× 2{× 2}× 2=× 2>× 2*× 1