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 value
1val double = { x: Int -> x * 2 }
// A lambda with two Int parameters that returns their sum
2val add = { a: Int, b: Int -> a + b }
3
// Call the lambda like a function: 5 * 2 prints 10
4println(double(5))
// 3 + 4 prints 7
5println(add(3, 4))

Output

10
7
Practice typing this code

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

More Kotlin examples at level 3

← Kotlin typing practice