Our task is to find in Kotline Sum of positive number in defined function. We written below code but getting compilation error. Hence need expertise advice for it -
import java.io.*
import java.math.*
import java.security.*
import java.text.*
import java.util.*
import java.util.concurrent.*
import java.util.function.*
import java.util.regex.*
import java.util.stream.*
import kotlin.collections.*
import kotlin.comparisons.*
import kotlin.io.*
import kotlin.jvm.*
import kotlin.jvm.functions.*
import kotlin.jvm.internal.*
import kotlin.ranges.*
import kotlin.sequences.*
import kotlin.text.*
fun sum(nums:Array<Int>,add: (acc:Int,ele:Int) -> Int):Int {
var res=0
for(i in nums){
res = add(res,i)
}
return res
}
--Editable Code---
fun getLambda(): (acc:Int,ele:Int) -> Int {
var lambda = { acc: Int, ele: Int -> acc + ele }
return lambda
}
---END--
fun main(args: Array<String>) {
val numsCount = readLine()!!.trim().toInt()
val nums = Array<Int>(numsCount, { 0 })
for (i in 0 until numsCount) {
val numsItem = readLine()!!.trim().toInt()
nums[i] = numsItem
}
val lambda = getLambda()
val result = sum(nums,lambda)
println(result)
}