Advertisement
jasurbekdev

Lambda in Kotlin

Aug 1st, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.32 KB | None | 0 0
  1. fun main() {
  2.     var list = (1..10).toList()
  3.     val sum = list.customSum { it % 2 == 1 }
  4.     println("The sum is: $sum")
  5. }
  6.  
  7. fun List<Int>.customSum(sumFunction: (Int) -> Boolean): Int {
  8.     var result = 0;
  9.     for(i in this) {
  10.         if (sumFunction(i)) {
  11.             result += i
  12.         }
  13.     }
  14.     return result
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement