with scope function in Kotlin
Level 4122 keystrokes
with(obj) { } runs a block with obj as this, so several members can be called without repeating the name, and returns the block's last expression. Here it adds 4 and 5 to the list and returns a summary string.
Kotlin code
// A mutable list with three elements1val nums = mutableListOf(1, 2, 3)2 // with takes the object as an argument; inside the block, this is nums3val result = with(nums) { // Calls nums.add(4) and nums.add(5) without writing nums4 add(4)5 add(5) // The last expression is the result; size is 5 and sum() is 156 "Size: $size, Sum: ${sum()}"7}8 // Print Size: 5, Sum: 159println(result)Output
Size: 5, Sum: 15
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.
(× 6)× 6,× 3:× 2"× 2{× 2}× 2=× 2$× 2