List Map in Kotlin

Level 398 keystrokes

Transforms each element of a list with the map function.

Kotlin code

// A list of the numbers 1 to 5
1val nums = listOf(1, 2, 3, 4, 5)
// map returns a new list with the lambda applied to each element
// it * it squares each value: [1, 4, 9, 16, 25]
2val squares = nums.map { it * it }
3
// Prints each square on its own line
4for (s in squares) println(s)

Output

1
4
9
16
25
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
  • (× 3
  • )× 3
  • =× 2
  • .× 1
  • {× 1
  • }× 1
  • *× 1

More Kotlin examples at level 3

← Kotlin typing practice